P 1308 statistics the number of words double pointer Solution

Meaning of the questions: Find an article in the number of times a word appears, case equivalent (ie a == A, and so the corresponding equivalence)

Input: The first line of a word

Second row: an article that contains a space input.

 

 

Double for the cycle of violence approach to find the time complexity of O (the n- 2 )

Solution: First two strings of words to uppercase or lowercase

           Find Next to start the match

           The first record matching the initial position of the first match to find success

Analysis time complexity: O (n)

Double pointer arithmetic:

        ALGORITHM: using the characteristic problem itself (sequence of incremental nature), using two subscripts i, j scanning sequence (which may be the same to the scan, reverse scan may be), to low complexity problem.

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  const  int MAXN = 1e6 + . 7 ;
 . 4  char Word [ 20 is ], sec [MAXN];
 . 5  int ANS, IDX, In Flag = . 1 ;
 . 6  void Reserve, ( char P [], int len)   // lowercase to uppercase 
. 7  {
 . 8      for ( int I = 0 ; I <len; I ++ )
 . 9          IF (P [I] <= ' Z 'P && [I]> = ' A ' ) P [I] - = 32 ;
 10  }
 . 11  
12 is  int main ()
 13 is  {
 14      the gets (Word), the gets (sec);   // input word, the input sentence 
15      int LEN1 strlen = (Word), LEN2 = strlen (sec);
 16      Reserve, (Word, LEN1), Reserve, (sec, LEN2);   // all uppercase 
. 17  
18 is      for ( int I = 0 , J = 0 ; I <LEN2 ;)   // double pointer practices 
. 19      {
 20 is          IF (sec [I + J] == Word [J] && (I>0   && sec [I- . 1 ] == '  ' )) J ++;   // character match where 
21 is          the else                                   // main string of letters corresponding to the word to be matched, when the sentence beginning letters have matching front a space 
22 is          {
 23 is              IF ( LEN1 && == J (I + J == LEN2 || sec [I + J] == '  ' )) // word match is found, the word has a space at the end of the sentence matches, or to the end of the sentence 
24              {
 25                  ANS ++;   / / word plus the number of occurrences. 1 
26 is                  IF (in Flag) IDX = I, in Flag = 0 ;   // record the first word successfully matched positions 
27  
28              }
 29              I ++;   //i after shifting the pointer 
30              J = 0 ;   // J back to 0 
31 is          }
 32      }
 33 is  
34 is      IF (ANS) the printf ( " % D% D \ n- " , ANS, IDX);
 35      the else the printf ( " -1 \ n- " );
 36      return  0 ;
 37 [ }

 

 

 

 

Guess you like

Origin www.cnblogs.com/Edviv/p/11762582.html