[PAT Basic] 1003. I want to pass!

By 1003 I want to! (20 points)

Source title

"Correct answer" is the most joy automatic reply sentence topic given system. This title belongs to the PAT of "correct answer" big delivery - just read the string following conditions are satisfied, the system will output "correct answer", otherwise outputs "Wrong Answer."

Get the "correct answer" conditions are:

  1. String must have only P, A,T three characters can not contain other characters;
  2. Arbitrary shape such as  xPATx a string can get "correct answer", wherein  x either the empty string, or only by the letter  A string thereof;
  3. If you  aPbTc are correct, then  aPbATca it is correct, which  abc  are either empty string, or only by the letter  A string composed.

Now ask you to write a PAT referee program automatically determines which strings can get "the answer right".

Input formats:

Each test comprises a test input. The first row is given a positive integer n-<10 , is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.

Output formats:

The detection result row for each string, if the string can get "correct answer", the output  YES, otherwise the output  NO.

Sample input:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

analysis:

  • Only one P a T
  • For xPATx, PAT, AAPATAA is YES, the same number as long as both ends of the A
  • For aPbTc and aPbATca,
    • PAT is correct, then PAAT is correct
    • AAPATAA is correct, then AAPAATAAAA is correct
  • Only a number of P and T, the number of front A * A number of intermediate A = back of: summarized
  • Can use three variables, front, mid, rear portion of each of the number A of the record, the final determination is equal to rear front * mid
 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 is          index ++ ;
 . 19          Front ++;     // number of records preceding A, 
20 is      }
 21 is      IF (STR [index] == ' P ' )
 22 is      {
 23 is          index ++ ;
 24      }
 25      the else 
26 is      {
 27          return  to false ;
 28      }
 29  
30      the while (STR [index] == ' A ' )
 31 is      {
 32         MID ++;     // number of records intermediate A, 
33 is          index ++ ;    
 34 is      }
 35  
36      IF (STR [index] == ' T ' )
 37 [      {
 38 is          index ++ ;
 39      }
 40      the else 
41 is      {
 42 is          return  to false ;
 43 is      }
 44 is  
45      the while ( STR [index] == ' a ' )
 46 is      {
 47          rEAR ++;     // number of records of the 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 solution:

At first understand  map  what is,  map  the C ++ STL containers, the internal implementation is a red-black tree (in the strict sense of a non-balanced binary tree, with a sorting function),  map to provide one to one relationship

 map <K, T>  class template is defined in the header file  map , which defines a preservation  T type of map object, each  T class object has a K type key associated . 

map features:

  • All the elements will be based on the elements of (key) key automatic sorting
  • map all elements are pair , have both (in real terms) key (key) and value
  • Regarded as the first element of a pair of key-key, the second element considered as a real-valued value
  • map does not permit two elements have the same key

The basic map constructor

map<int ,string >intMap;

map<string , int >strMap;        

map< char ,string>charMap;

map<sring, char>strMap;        

map<char ,int>charMap;           

map<int ,char >intMap;

Basic operation functions of the map

      begin () returns a pointer to the head of map iterators 

      clear () removes all elements 

      count () returns the number specified element appears 

      empty () is empty map true if it returns 

      end () returns a pointer to the end of the map iterators 

      equal_range () returns iterator particular item to 

      delete erase () element 

      find () to find an element 

      get_allocator () returns the map configurator 

      insert () element inserted 

      key_comp () function returns the key element of Comparative 

      lower_bound () returns the key-value > = a given the first element in a position 

      MAX_SIZE () returns the maximum number of elements can be accommodated 

      rbegin () returns a pointer to the tail of the reverse map iterator 

      rend () returns a pointer to the head of reverse iterator map 

      size () returns the elements in the map The number of 

      swap () exchanging two map

      upper_bound, () Returns the key value > given element in a first position 

      value_comp () function returns the value of the comparison element
View Code

 

Ideas:

The P, A, T three characters as a key stored map, the corresponding value is the number of times they appear and, finally determines map.size () is equal to 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 is          charmap [STR [i]] ++ ;
 14          IF (STR [i] == ' P ' )
 15          {
 16              posOfP = i;     // this time P i is the number of A's front (executing the loop step will be performed ++ i) can be understood as the position P 
. 17          }
 18 is          IF (STR [I] == ' T ' )
 . 19          {
 20 is              posOfT = I;     // I to position 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 lines of code:

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 

in order to prevent or `PT` number P and T greater than 1
and charMap.size () == 3 is to prevent the occurrence of letters other than PAT

 

posOfT - posOfP != 1 && posOfP*(posOfT - posOfP - 1) == str.size() - posOfT - 1)
1. Assuming a given string AAPATAA, then posOfT = 4, posOfP = 2, posOfT - posOfP = 2, therefore, the number of A = posOfT intermediate - posOfP -. 1 
2. Suppose given string is PT, then posOfT - posOfP = 1, so posOfT - posOfP = 1 is not intended to exclude such intermediate PT a situation!
3. Suppose given string is AAPATAA, then str.size () = 7, posOfT = 4, str. a 1 = the number of back - size () - posOfT

Satisfies the above condition returns true

 

Guess you like

Origin www.cnblogs.com/47Pineapple/p/11360492.html