您的位置:首页 > 其它

洛谷P1308 统计单词数

2018-03-18 12:41 169 查看
昨天比完赛就发现自己实在渣爆了(之前就知道不过这次这种感觉到达顶峰了啊。。字符串,贪心这种基础题目一直都没有掌握,一直在盲目地追求算法,现在要多补一补才行啊 洛谷P1308
这道题算是刷知识点的一道题了,之前从来没做过,学到了两个函数,getline(),find()getline详解, find详解tolower() 变为小写字母, toupper() 变为大写字母关于getline,一次读入一行,还是比较刺激的,之前一直很烦读入空格,这次好了。。。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;

int main()
{
int pos1 = -1, pos2, cnt = 0;
int ans;
string str, str1, str2;
getline(cin, str);
getline(cin, str1);
str1 = str1 + ' ';
for(int i = 0; i < str.size(); i ++)
//if(str[i]>='A'&&str[i]<='Z')
str[i] = tolower(str[i]);
for(int i = 0; i < str1.size(); i ++)
//if(str1[i]>='A'&&str1[i]<='Z')
str1[i] = tolower(str1[i]);
while(1)
{
pos2 = str1.find(' ', pos1 + 1);//寻找空格位置
str2 = str1.substr(pos1 + 1, pos2 - pos1 - 1);//提取两个空格之间的字符串
if(str == str2)
{
if(cnt == 0)ans = pos1 + 1;
cnt ++;
}
pos1 = pos2;
if(pos1 == -1)break;//找不到空格时find返回-1
}
if(! cnt)printf("-1\n");
else printf("%d %d\n", cnt, ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: