HDU 2072单词数
来源:互联网 发布:血色天都 知乎 编辑:程序博客网 时间:2024/05/21 09:46
题目链接:HDU2072
题目意思:求每一行不同单词数解题思路:利用STL中的set保存取到的单词输出set.size()
Code:
#include<iostream>#include<ctype.h>#include<cstdio>#include<set>using namespace std;int main(){ set<string> Set; char c; while((c=getchar())!='#') { string s=""; while(c!='\n') { if(c!=' ') s+=c; while((c=getchar())!=' '&&c!='\n') s+=c; if(s.length()) Set.insert(s); s=""; } cout<<Set.size()<<endl; Set.clear(); }}
需要注意的是:如果把if(c!=’ ‘) s+=c;中的判断条件去掉,则“ my friend my”输出为3,而不是正确结果2,因为当行首有空格时,错误的解法会把空格(仅为第一个)加进第一个字符串。
若是求每行单词数,不必考虑相同与否,则题目更为简单。
Code:
#include<iostream>#include<ctype.h>#include<cstdio>using namespace std;int main(){ char s[512]; s[0]=' '; while(true) { gets(s+1); int count=0; if(s[1]=='#') break; for(int i=1;s[i];i++) if(isalpha(s[i])&&s[i-1]==' ') count++; cout<<count<<endl; }}
0 0
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- Hdu 2072 - 单词数
- Hdu 2072 单词数
- hdu 2072-单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- java开发——《问题总结一》
- codeforces 245H H. Queries for Number of Palindromes(区间dp)
- 2014华中科技大学复试
- Integral of sin(x)/x
- ios 一些常用的第三方库
- HDU 2072单词数
- Android之ArrayAdapter的详解
- 2045不容易系列之(3)—— LELE的RPG难题
- R语言利用ggplot画图时调整坐标轴字体大小
- 【打CF,学算法——三星级】Codeforces 9C Hexadecimal's Numbers (解法汇总)
- XAMPP for MAC 安装后MYSQL不能正常启动的解决办法
- Eclipse ADT Tips
- android ActionBar 兼容低版本之搜索功能
- 面向对象三大基本特性,五大基本原则