首页 技术 正文
技术 2022年11月11日
0 收藏 984 点赞 4,357 浏览 2536 个字

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

得到“答案正确”的条件是:

1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入格式:每个测试输入包含1个测试用例。第1行给出一个自然数n(<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。

输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。

输入样例:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO

初看题目我是这么理解的:

  • 由条件 (1) (2) 知,有 2 种基本情况是 “正确” 的,即 PAT 和 xPATx;
  • 由条件 (3) 知,其余 “正确” 情况都是由那 2 种基本情况不断演变而来。

因此我想到的是递归暴力法:

  • 首先判断 2 种基本情况为 “正确”。
  • 要想 aPbATca “正确”,就要 aPbTc “正确”,于是用 string.find() 找到 aPbATca 中 ‘P’ 和 ‘AT’ 的下标,通过 string.substr() 截取字符串获得 aPbTc,递归判断 aPbTc 是否 “正确”。
 #include <iostream>
#include <string> using namespace std; bool onlyA(string s) {
for (int i = ; i < s.size(); i++)
if (s[i] != 'A')
return false;
return true;
} bool isCorrect(string s) {
if (s == "PAT") return true;
else if (s.find("PAT") != string::npos) {
int idx = s.find("PAT");
string x1 = s.substr(, idx), x2 = s.substr(idx + );
if (onlyA(x1) && onlyA(x2) && x1 == x2)
return true;
}
else if (s.find("P") != string::npos) {
int p_idx = s.find("P");
string a1 = s.substr(, p_idx);
string a2 = s.substr(s.size() - p_idx);
if (onlyA(a1) && onlyA(a2) && a1 == a2 && s.find("AT") != string::npos) {
int at_idx = s.find("AT");
string b = s.substr(p_idx + , at_idx - p_idx - );
string ca = s.substr(at_idx + );
string c = ca.substr(, ca.size() - a2.size());
if (isCorrect(a1 + "P" + b + "T" + c))
return true;
}
else {
return false;
}
}
return false;
} int main() {
int n;
cin >> n;
string *s = new string[n];
for (int i = ; i < n; i++)
cin >> s[i];
for (int i = ; i < n; i++) {
if (isCorrect(s[i]))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return ;
}

后来查看网上解析,总结题意发现:

  1. 基本情况是 xPATx,x 只含有 ‘A’ 或为空。这里我们用 a、b、c 表示原题中相应位置  ‘A’ 的数量,则初始情况下,a = c,b = 1。
  2. 由条件(3),’P’ 和 ‘T’ 中间每增加 1 个 ‘A’,即 b + 1 后,整个字符串结尾要添加 1 个 ‘P’ 前的字符串,即 c 变为 c + a。
    由初始状态 b = 1,且若 b = b + 1,则 c = c + a,可知 c = a × b

挖掘到这个深层信息,改写 isCorrect() 函数如下,效率会更高。

// C style
bool isCorrect(string s) {
int a = , b = , c = ;
bool hasP = false, hasAT = false;
for (int i = ; i < s.size(); i++) {
if (!hasP) {
if (s[i] == 'A') {
a++;
continue;
}
else if (s[i] == 'P') {
hasP = true;
continue;
}
else {
return false;
}
}
else {
if (!hasAT) {
if (s[i] == 'A') {
b++;
continue;
}
else if (s[i] == 'T' && s[i - ] == 'A') {
hasAT = true;
continue;
}
else {
return false;
}
}
else {
if (s[i] == 'A') {
c++;
continue;
}
else {
return false;
}
}
}
}
if (hasP && hasAT && a * b == c)
return true;
else
return false;
}// C++ style
bool isCorrect(string s) {
int idxP = s.find("P"), idxAT = s.find("AT");
if (idxP != string::npos && idxAT != string::npos) {
for (int i = ; i < idxP; i++) {
if (s[i] != 'A')
return false;
}
for (int i = idxP + ; i < idxAT; i++) {
if (s[i] != 'A')
return false;
}
for (int i = idxAT + ; i < s.size(); i++) {
if (s[i] != 'A')
return false;
}
// a * b == c
if (idxP * (idxAT - idxP) == (s.size() - idxAT - ))
return true;
}
return false;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291