Count words
Time Limit: 1 s
Memory limit: 128 MB
[Problem description]
Common text editors have the word search function, which can quickly locate the bit of a specific word in an article.
Some can also count the number of times a specific word appears in the article.
Now, please program to implement this function. The specific requirement is: to give a word, please output it in the given Article
The number of occurrences and the position where the first occurrence occurs. Note: words are not case sensitive, but must be exactly matched,
That is to say, the given word must be exactly the same as an independent word in the document without case sensitivity (see example 1 ),
If a given word is only part of a word in the document, it is not matched (see example 2 ).
[Input]
The input file name is stat. In, two rows.
1st act as a string containing only letters, indicating a given word;
2nd act as a string, which may only contain letters and spaces, indicating a given article.
[Output]
The output file name is stat. Out.
There is only one row. If a given word is found in the article, two integers are output, which are separated by a space,
The number of times a word appears in an article and the position where it appears for the first time.
Position of the parent in the article, starting from 0). If a word does not appear in the article, an integer-1 is output directly.
Input and Output Example 1
Input:
To
To be or not to be is a question
Output:
2 0
[Input and Output Example 1]
The output result indicates that the given word to appears twice in the article, and the first occurrence is 0.
Input and Output Example 2
Input:
To
Did the Ottoman Empire lose its power at that time
Output:
-1
[Example 2 of input and output]
Indicates that the given word to does not appear in the article, and the output integer is-1.
[Data Scope]
1 ≤ word length ≤ 10.
1 ≤ Article length less than or equal to 1,000,000.
1 # include <stdio. h> 2 # include <string. h> 3 int main () 4 {5 char S1 [100], S2 [1000]; 6 int F1; 7 long first =-1, num = 0; // first: the subscript that appears for the first time. Num: Total number of occurrences 8 char ch; 9 int I; 10 int Index =-1; // indicates the subscript 11 12 freopen ("stat2.in ", "r", stdin); 13 freopen ("stat2.txt", "W", stdout); 14 15 scanf ("% s", S1); 16 getchar (); // absorb carriage return character 17 for (I = 0; S1 [I]! = '\ 0'; I ++) 18 {19 if (S1 [I]> = 'A' & S1 [I] <= 'Z ') // if it is an upper-case letter, the lower-case letter is 20 S1 [I] = S1 [I] + 32; 21} 22 23 F1 = 0; // you have not started to enter the new word 24 I = 0 in the article; // The subscript 25 while (CH = getchar () in the input character of the article is saved in the S2 array ())! = EOF) 26 {27 index ++; 28 If (CH = '') 29 {30 if (F1 = 0) {continue ;} 31 else32 {33 S2 [I] = '\ 0'; 34 if (strcmp (S1, S2) = 0) 35 {36 IF (first =-1) 37 first = index-strlen (S2); 38 num ++; 39} 40 I = 0; 41 F1 = 0; 42} 43} 44 else45 {46 F1 = 1; 47 If (CH> = 'A' & Ch <= 'Z') CH = CH + 32; 48 S2 [I] = CH; 49 I ++; 50} 51} 52 If (Num> 0) 53 printf ("% d \ n", num, first); 54 else printf ("-1 \ n "); 55 return 0; 56}
View code
Noip2011 popularity group -- count words