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).
-
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
Ideas:
Large simulation;
Come on, on the code:
#include <map>#include<Set>#include<queue>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intLen_start,len_now,times_all=0, ans=0, ans_first=0x7fffffff;Charstart[1000001],now[1000001];voidIF_OK (intNow_) { if((now_==0|| now[now_-1]==' ') && (now[now_+len_start]==0|| now[now_+len_start]==' ')) { BOOLIf_add=true; for(intI=0; i<len_start;i++) { if(Now[now_+i]==start[i])Continue; If_add=false; Break; } if(If_add) {ans++; Ans_first=min (ans_first,now_); }}}inlinevoidChar_ (Char&char__) { if(char__>='A'&&char__<='Z') char__+= +;}intMain () {gets (start); Gets (now); Len_start=strlen (start), len_now=strlen (now); for(intI=0; i<len_start;i++) Char_ (Start[i]); for(intI=0; i<len_now;i++) Char_ (Now[i]); //printf ("%s\n", start); //printf ("%s\n", now); for(intI=0; i<=len_now-len_start;i++) { if(now[i]==start[0]) IF_OK (i); } if(ans==0) printf ("-1\n"); Elseprintf"%d%d\n", Ans,ans_first); return 0;}
AC Diary--Count words Openjudge 1.12 5