P1003 I want to pass!

  This question may to death of me, I looked for the third condition was read. When I read the third condition, I felt so stupid before. Well, more than that, the first question

  The sample is longer, so the comparison may be a little fuzzy. Subject requirements are very clear, three conditions are met, any of the conditions are seen as the answer is correct, then the output OK. Look after the needs, we analyze the conditions.

condition:

  The first: containing only P, A, T three characters, a few will not, this means that it is at least the length of the string of PAT 3.

  Second: the form "......" the PAT "......" string of the form, before and after "......" of the characters and the length must be equal only by a string consisting of A,

    That is shaped like APATA, AAPATAA, such strings AAAPATAAA ......, it is also determined that the answer is correct

  Third: shaped like aPbTc string if they meet the above two, then aPAbTac also "correct answer."

 We'll have to analyze all the third condition, a separate analysis of the third condition will find that no matter what the input is OK. Therefore, we should analyze the combined sample.

    According to the sample in the three groups of samples:

       AAPATAA    --YES;

      AAPAATAAAA  --YES;

      APAAATAA   --NO。

  So that we can draw about the law

  只要P前面的A的个数乘以PT之间的A的个数的积等于T后面的A的个数且字符串中只含有单个P和T就为YES,否则NO。即条件三中符合a*b=c然后看条件二不难发现:条件二包含在条件三中。知道这些后,就可以通过了。

  Finally, of course, it is to achieve it:

Code:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define LEN 101
 4 
 5 int main()
 6 {
 7     int cnt_str = 0; //字符串个数
 8     scanf("%d", &cnt_str);
 9     for (int i = 0; i < cnt_str; i++)
10     {
11         char str[LEN];
12         scanf("%s", str); // read the string 
13 is          int CNT_P = 0 , cnt_T = 0 , = isRight . 1 ;
 14          int POS_P = 0 , pos_T = 0 ; // set P, the subscript T is 
15          for ( int I = 0 ; I <strlen (STR); I ++ )
 16          {
 . 17              IF (STR [I] == ' P ' )
 18 is              {
 . 19                  POS_P = I; // record the subscript P 
20 is                  CNT_P ++ ;
 21 is             }
 22 is              the else  IF (STR [I] == ' T ' )
 23 is              {
 24                  pos_T = I; // record the subscript T 
25                  cnt_T ++ ;
 26 is              }
 27              the else  IF (! STR [I] = ' A ' )
 28              { // other characters 
29                  isRight = 0 ;
 30                  BREAK ;
 31 is              }
 32          }
 33 is          IF(isRight && CNT_P == . 1 && cnt_T == . 1 )
 34 is          {                                   // If the character string is only three kinds of PAT and P, T is the number. 1 
35              int L_A = 0 , C_a = 0 , r_A = 0 ; // declaration left , the number of the right character a 
36              L_A = POS_P;
 37 [              C_a = pos_T - POS_P - . 1 ;
 38 is              r_A = strlen (STR) - pos_T - . 1 ;
 39              IF (C_a> 0 && * L_A C_a == r_A)
 40              { //Between PT A, and A is the number of left and right * in = 
41 is                  the printf ( " YES \ n- " );
 42 is                  Continue ;
 43 is              }
 44 is          }
 45          the printf ( " NO \ n- " );
 46 is      }
 47      return  0 ;
 48 }
View Code

  Algorithm is not easy, gentlemen encourage each other!

Guess you like

Origin www.cnblogs.com/daker-code/p/11618534.html