云计算百科
云计算领域专业知识百科平台

P1308 [NOIP 2011 普及组] 统计单词数

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

void toLower(string& str) {
for(char& c:str) {
c = tolower(c);
}
}

int main() {
string sub;

cin>>sub;
toLower(sub);

string str;
cin.ignore();
getline(cin, str);
toLower(str);

size_t pos = 0;
int cnt = 0;
int first = -1;
while((pos = str.find(sub, pos)) != string::npos) {
int len = sub.size();
if(pos == 0) {
if(str[pos+len]==' ') {
cnt++;
if(first==-1) first = pos;
}
} else {
if(str[pos-1]==' '&&str[pos+len]==' ') {
cnt++;
if(first==-1) first = pos;
}
}
pos += len;
}

if(cnt!=0) {
cout<<cnt<<" "<<first<<endl;
} else {
cout<<-1<<endl;
}

return 0;
}

赞(0)
未经允许不得转载:网硕互联帮助中心 » P1308 [NOIP 2011 普及组] 统计单词数
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!