[PAT Basic] 1003.我要通过!

匿名 (未验证) 提交于 2019-12-02 23:54:01

题目来源

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

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

  1. 字符串中必须仅有 PAT这三种字符,不可以包含其它字符;
  2. xPATxxA
  3. aPbTcaPbATcaabcA

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

输入格式:

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

输出格式:

YESNO

输入样例:

8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA 

输出样例:

YES YES YES YES NO NO NO NO

分析:

  • 只能有一个P一个T
  • 对于 xPATx,PAT, AAPATAA都是YES,只要两端的 A 的个数相同
  • 对于aPbTc和aPbATca,
    • PAT是正确的,那么PAAT也是正确的
    • AAPATAA是正确的,那么AAPAATAAAA也是正确的
  • 总结:只能有一个P和T,前面A的个数 * 中间A的个数 = 后面A的个数
  • 可以用三个变量,front,mid,rear将每个部分的A的个数记录下来,最后判断front * mid是否等于rear
 1 #include <iostream>  2 #include <string>  3 using namespace std;  4   5 bool pass(string str)  6 {  7     if (str == "PAT")  8     {  9         return true; 10     } 11      12     int index, front, mid, rear; 13     index = front = mid = rear = 0; 14  15      16     while (str[index] == 'A') 17     { 18         index++; 19         front++;    //记录前面A的个数 20     } 21     if (str[index] == 'P') 22     { 23         index++; 24     } 25     else 26     { 27         return false; 28     } 29  30     while (str[index] == 'A') 31     { 32         mid++;    //记录中间A的个数 33         index++;     34     } 35  36     if (str[index] == 'T') 37     { 38         index++; 39     } 40     else 41     { 42         return false; 43     } 44  45     while (str[index] == 'A') 46     { 47         rear++;    //记录后面A的个数 48         index++; 49     } 50  51     if (!str[index] && rear == mid * front && mid > 0) 52     { 53         return true; 54     } 55      56     return false; 57 } 58  59 int main() 60 { 61     int n; 62     string str; 63     cin >> n; 64  65     for (int i = 0; i < n; ++i) 66     { 67         cin >> str; 68         bool flag = pass(str); 69         if (flag) 70         { 71             cout << "YES" << endl; 72         } 73         else 74         { 75             cout << "NO" << endl; 76         } 77     } 78  79     return 0; 80 }

map解法:

map 提供一对一的关系

map<K, T>TT 类对象都有一个关联的K类型的键

map的特性:

  • 所有的元素会根据元素的键值(key)自动排序
  • map所有元素都是pair,同时拥有key(键值)和value(实值)
  • pair的第一个元素视为键值key,第二个元素视为ʵֵvalue
  • map不允许两个元素拥有相同的键值

map的基本构造函数

map<int ,string >intMap;  map<string , int >strMap;          map< char ,string>charMap;  map<sring, char>strMap;          map<char ,int>charMap;             map<int ,char >intMap;

map的基本操作函数

      begin()          返回指向map头部的迭代器        clear()         删除所有元素        count()          返回指定元素出现的次数        empty()          如果map为空则返回true        end()            返回指向map末尾的迭代器        equal_range()    返回特殊条目的迭代器对        erase()          删除一个元素        find()           查找一个元素        get_allocator()  返回map的配置器        insert()         插入元素        key_comp()       返回比较元素key的函数        lower_bound()    返回键值>=给定元素的第一个位置        max_size()       返回可以容纳的最大元素个数        rbegin()         返回一个指向map尾部的逆向迭代器        rend()           返回一个指向map头部的逆向迭代器        size()           返回map中元素的个数        swap()            交换两个map        upper_bound()     返回键值>给定元素的第一个位置        value_comp()      返回比较元素value的函数
View Code

思路:

将 P, A, T 三个字符作为键值存入map,对应的value是它们出现的次数,最后还要判断 map.size()是否等于3

 1 #include <iostream>  2 #include <string>  3 #include <map>  4 using namespace std;  5   6 bool pass(string str)  7 {  8     map<char, int> charMap;  9     int length = str.size(); 10     int posOfP = 0, posOfT = 0; 11     for (int i = 0; i < length; ++i) 12     { 13         charMap[str[i]]++; 14         if (str[i] == 'P') 15         { 16             posOfP = i;    //这时候的i为P前面 A 的个数(执行完循环体的步骤才会执行++i)也可以理解为P的位置 17         } 18         if (str[i] == 'T') 19         { 20             posOfT = i;    //i为T的位置 21         } 22     } 23     if (charMap['P'] == 1 && charMap['T'] == 1 && charMap['A'] != 0 && charMap.size() == 3 && posOfT - posOfP != 1 && posOfP*(posOfT - posOfP - 1) == str.size() - posOfT - 1) 24     { 25         return true; 26     } 27     else 28     { 29         return false; 30     } 31 } 32  33  34 int main() 35 { 36     int n; 37     string str; 38     cin >> n; 39  40     for (int i = 0; i < n; ++i) 41     { 42         cin >> str; 43         bool flag = pass(str); 44         if (flag) 45         { 46             cout << "YES" << endl; 47         } 48         else 49         { 50             cout << "NO" << endl; 51         } 52     } 53     return 0; 54 }

23行代码:

if (charMap['P'] == 1 && charMap['T'] == 1 && charMap['A'] != 0 && charMap.size() == 3 && posOfT - posOfP != 1 && posOfP*(posOfT - posOfP - 1) == str.size() - posOfT - 1)
charMap['P'] == 1 && charMap['T'] == 1 && charMap['A'] != 0 && charMap.size() == 3为了防止出现`PT` 或 P和T个数大于1的情况而charMap.size() == 3 是为了防止出现除 PAT 之外的字母

posOfT - posOfP != 1 && posOfP*(posOfT - posOfP - 1) == str.size() - posOfT - 1)
1.假设给定字符串是AAPATAA,那么posOfT = 4, posOfP = 2, posOfT - posOfP = 2,所以,中间A的个数 = posOfT - posOfP - 12.假设给定字符串是PT,那么 posOfT - posOfP = 1,所以posOfT - posOfP != 1是用来排除PT这种中间没有A的情况的3.假设给定字符串是AAPATAA,那么str.size() = 7, posOfT = 4, str.size() - posOfT - 1 = 后面A的个数

满足上述条件就返回 true

来源: https://www.cnblogs.com/47Pineapple/p/11360492.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!