05: count words, 05 count words
05: count words
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65536kB
-
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
-
2 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
-
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
-
Example #1: Toto be or not to be is a question example #2: toDid the Ottoman Empire lose its power at that time
-
Sample output
-
Example #0 example #2:-1
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 string a; 7 string b; 8 int tot; 9 char kong[4];10 int main()11 {12 cin>>a;13 getline(cin,b);14 getline(cin,b);15 int la=a.size();16 for(int i=0;i<la;i++)17 {18 if(a[i]>=65&&a[i]<=90)19 {20 a[i]=a[i]+32;21 }22 }23 int lb=b.size();24 for(int i=0;i<lb;i++)25 {26 if(b[i]>=65&&b[i]<=90)27 {28 b[i]=b[i]+32;29 }30 }31 a=' '+a+' ';32 b=' '+b+' ';33 int wz=b.find(a);34 if(wz==-1)cout<<-1;35 else36 {37 int ans=wz;38 while(wz!=-1)39 {40 tot++;41 wz=b.find(a,wz+1);42 }43 cout<<tot<<" "<<ans;44 }45 return 0;46 }