131 count words
Noip National League popularity group in 2011
Time Limit: 1 s space limit: 128000 kb title level: Silver silver Title Description
Description
Common text editors have the word search function, which can quickly locate a specific word in the article, and some can also count the number of times a specific word appears in the article.
Now, you can program to implement this function. The specific requirement is: to give a word, please output the number of times it appears in the given Article and the position it appears for the first time. 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 description
Input description
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 description
Output description
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 the word appears in the article and the position where it appears for the first time (that is, when the first word appears in the article, the position of the first letter in the article starts from 0 ); if a word does not appear in an article, an integer-1 is output directly.
Sample Input
Sample Input
Input 1:
To
To be or not to be is a question
Input 2:
To
Did the Ottoman Empire lose its power at that time
Sample output
Sample output
Output 1:
2 0
Output 2:
-1
Data range and prompt
Data size & hint
Data range
1 ≤ word length ≤ 10.
1 ≤ Article length less than or equal to 1,000,000.
My string processing has become a dog.
Questions in the TMD popularization group can be difficult for me for a long time.
Rely on me
1 # include<cstdio> 2 # include<cctype> 3 # include<string> 4 # include<cstring> 5 # include<iostream> 6 # include<algorithm> 7 using namespace std; 8 string st1,st2; 9 int tot,fist=-1;10 int main(){11 getline(cin,st1);12 getline(cin,st2);13 int len1=st1.size();14 int len2=st2.size();15 for(int i=0;i<len1;i++)st1[i]=tolower(st1[i]);16 for(int i=0;i<len2;i++)st2[i]=tolower(st2[i]);17 for(int i=0;i<len2;i++)18 if(st2[i]==st1[0]&&st2[i-1]==‘ ‘||i==0){19 int k=0,vis=0;20 for(int j=i;j<len2&&st2[j]!=‘ ‘;j++)if(st2[j]!=st1[k++]){vis=1;break;}21 if(vis==0&&k==len1){tot++;if(fist==-1)fist=i;}22 }23 if(tot)cout<<tot<<" "<<fist; 24 else cout<<"-1";25 return 0;26 }
[String processing] codevs 1131 counts the number of words