洛谷P1308 统计单词数
2018-03-18 12:41
169 查看
昨天比完赛就发现自己实在渣爆了(之前就知道不过这次这种感觉到达顶峰了啊。。字符串,贪心这种基础题目一直都没有掌握,一直在盲目地追求算法,现在要多补一补才行啊 洛谷P1308
这道题算是刷知识点的一道题了,之前从来没做过,学到了两个函数,getline(),find()getline详解, find详解tolower() 变为小写字母, toupper() 变为大写字母关于getline,一次读入一行,还是比较刺激的,之前一直很烦读入空格,这次好了。。。
这道题算是刷知识点的一道题了,之前从来没做过,学到了两个函数,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; }
相关文章推荐
- 洛谷P1308 统计单词数
- 洛谷P1308 统计单词数
- 洛谷P1308 统计单词数
- 统计单词个数
- C语言:输入一行字符,统计其中有多少单词,单词之间用空格分隔开
- 使用伸展树(SplayTree)统计单词频率的Java实现
- 利用visual VM 分析统计单词频率程序
- 【python】统计文本中出现最多次的单词
- 利用VS2012对文本文件的单词频率统计,并分析结果
- 【codevs1040】【01NOIPTG】统计单词个数,字符串的划分DP
- NOIP 统计单词个数
- Java编程:统计文本文件中单词出现频率
- 算法提高 统计单词数
- 函数式编程实践记(1)——统计单词频率
- shell统计文本中单词的出现次数
- 统计单词次数
- 单词统计
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
- 多线程统计多个文件的单词数目
- C语言实验——单词统计(java)