05: Count the number of words
-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
Describe
-
The general text editor has the ability to find words, which can quickly locate the position of a particular word in the article, and some can also count the number of occurrences of a particular word in the article.
Now, you are programmed to implement this function, the specific requirement is: Given a word, please output it in the given article the number of occurrences and the first occurrence of the position. Note: Matching words is not case-sensitive, but requires an exact match, that is, a given word must be exactly the same as a separate word in the story (see Example 1) If the given word is only part of a word in the story (see Example 2). The length of the article is within 10^6, the word length is less than 10.
-
Input
-
2 lines.
The 1th act is a string that contains only letters that represent a given word;
The 2nd behavior is a string that may contain only letters and spaces that represent a given article.
-
Output
-
Only one row, if you find a given word in the article output two integers, two integers separated by a space, respectively, the number of occurrences of the word in the article and the first occurrence of the position (that is, in the first occurrence of the article, the position of the first letter in the article, position from 0); If the word doesn't appear in the article, An integer-1 is output directly.
-
Sample input
-
Example #1: Toto be OR isn't is a question sample #2: Todid The Ottoman Empire lose its power at that time
-
Sample output
-
Sample #1:2 0 Examples #2:1
-
Source
The
-
second problem of popularization group of NOIP2011 semi-finals
1#include <stdio.h>2#include <string.h>3 intMain ()4 {5 Charstr1[ the],str2[1000005],str3[ the]="";6 intCount=0, firstposition=0;7 Char*p=NULL;8 intI,j,flag;9 Tenscanf"%s", str1); OneGetChar ();//receive scanf ("%s", str1), carriage return symbol A gets (str2); - -p=str1; the while(*p!=' /')//convert str1 to lowercase -{if(*p>='A'&&*p<='Z') *p=*p+ +; p++;} - -p=str2; + while(*p!=' /')//convert str2 to lowercase -{if(*p>='A'&&*p<='Z') *p=*p+ +; p++;} + AI=0; atj=0; -flag=0;//I haven't found the first place yet. - while(str2[i]!=' /') - { - if(str2[i]>='a'&&str2[i]<='Z') - { instr3[j]=Str2[i]; -i++; toJ + +; + } - Else//encounters a non-alphabetic character that is considered to constitute a new word the { *str3[j]=' /'; $ if(strcmp (STR1,STR3) = =0)//if the new word matches the word you need to findPanax Notoginseng { -count++; the if(flag==0)//Have not found the first occurrence of the position, this time is the first time to find the word + { Afirstposition=i-strlen (str1); theflag=1; + } - } $j=0; $i++; - } - } the if(flag==0) printf ("-1\n"); - Else Wuyiprintf"%d%d\n", count,firstposition); the return 0; -}
05: Count The number of words "the second problem of the popularization group of NOIP2011"