luogu part 1.3 count the number of words (string based)

P1308 [NOIP2011 Popularity Group] Count the number of words

Expand
Title description
General text editors have a word search function, which can quickly locate the position of a specific word in the article, and some can also count the number of times a specific word appears in the article.

Now, please program to achieve this function. The specific requirements are: Given a word, please output the number of times it appears in a given article and the position of its first occurrence. Note: When matching words, it is not case-sensitive, but it requires an exact match, that is, the given word must be exactly the same as an independent word in the article without being case-sensitive (see example 1), if a word is given Only part of a word in the article is not considered a match (see example 2).

The input format is
22 lines in total.

The eleventh line is a string containing only letters, indicating a given word;

The 22nd line is a string, which may only contain letters and spaces, indicating a given article.

The output format is
one line. If a given word is found in the article, it will output two integers, separated by a space between the two integers, which are the number of times the word appears in the article and the position of the first occurrence (ie in the article When it first appears, the position of the first letter of the word in the article, starting from 00); if the word does not appear in the article, directly output an integer -1−1.

Input and output sample
input #1 copy
To
to be or not to be is a question
output #1 copy
2 0

Input #2 copy
to
Did the Ottoman Empire lose its power at that time
output #2 copy
-1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int main() {
    
    
	string s, s0;
	cin >> s0;
	getchar();
	for (int i = 0; i < s0.size(); i++){
    
    
		if (s0[i] <= 'Z' && s0[i] >= 'A')s0[i]+='a' - 'A';
	}
	getline(cin, s);
	for (int i = 0; i < s.size(); i++){
    
    
			if (s[i] <= 'Z' && s[i] >= 'A')s[i]+='a' - 'A';
		}
		s=' '+s+' ';//避免样例二
		s0=' '+s0+' ';
	if(s.find(s0)==string::npos){
    
    
		cout<<-1<<endl;
	}
	else {
    
    
		int a=s.find(s0);
		int b=a,t=0;
		while(b!=string ::npos){
    
    //string::npos
			t++;
			b=s.find(s0,b+1);
		}
		cout<<t<<" "<<a<<endl;
	}

	return 0;

}

Guess you like

Origin blog.csdn.net/Minelois/article/details/113763705