hdu 2072 找单词 很不错的一道题 模拟 and STL 2种做法
来源:互联网 发布:网络客服是做什么的 编辑:程序博客网 时间:2024/05/18 14:12
单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16105 Accepted Submission(s): 4187
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend#
Sample Output
4
Author
Lily
Source
浙江工业大学网络选拔赛
Recommend
linle
思路:
这里卡人的数 据有 2个单词中间有多个空格 结尾也有空格
我白白错了4次 所以这种题 宁可麻烦也要把所有的情况考虑到
#include<stdio.h>#include<map>#include<string>#include<string.h>using namespace std;char s[100000];int main(){ int d,cnt,flag=0; char ch; map<string,int>mp; while(1)//一开始编其它题目的时候发现把ch!='\n'放这里就弹出错误窗口 { while(1) { ch=getchar(); if(ch=='#') {flag=1;break;} cnt=0; while(1)//不要把条件放在这里哦 放在里面 否则出错 我不知道为什么 纠结 { if(ch!=' ') break; ch=getchar(); } if(ch=='\n') { printf("%d\n",mp.size()); mp.clear();continue; } s[cnt]=ch; // while(ch=getchar()&&ch!=' ') 错误同上 所以 只要是ch=getchar 其它条件尽量放进{}内 while(ch=getchar()) { if(ch==' '||ch=='\n') break; s[++cnt]=ch; } s[++cnt]='\0'; string s1=s; mp[s]=1;//有相同单词的时候 mp数组方式添加 自动覆盖前面相同单词 if(ch=='\n') { printf("%d\n",mp.size()); mp.clear(); } } if(flag) break; } return 0;}STL 很帅的做法
#include <iostream>#include <map>#include <stdio.h>#include <string.h>#include<string>using namespace std;int main(){ char ch[1000000]; string str,l; int m,i,a,len,q; while(gets(ch)&&ch[0]!='#') { a=0;m=0;q=0;len=0; map<string,int>mp; map<string,int>::iterator p;l=ch;i=0;while(ch[i]==' ') i++; //可能有前导空格 这里很坑爹if(i==strlen(ch)) {printf("0\n");continue;} for(i;i<strlen(ch)+1;i++) //这里加1 是为了比如 asb sfd这种数据当处理sfd的时候能进入else下面的处理 { if(ch[i]>='a'&&ch[i]<='z') { len++; q=0; } else { if(q==0){a=i-len;str=l.substr(a,len);//cout<<str<<endl;mp[str]=1;len=0;q=1;} } } printf("%d\n",mp.size()); }}
感悟 :考虑所有情况 考虑要严谨周密 前导空格 后缀空格 中间空格 对于所有题都要抱着严谨周密的态度
- hdu 2072 找单词 很不错的一道题 模拟 and STL 2种做法
- 一道很不错的字符分割题
- hdu 2072 找不同的单词数
- hdu 2082 找单词(模拟)
- Python解一道题的N种做法(2)
- hdu 5402 一道神奇的模拟题
- hdu2577一道很不错的DP
- 一道很不错的js面试题
- HDU 2072 单词数【STL】
- Python解一道题的N种做法(1)
- [orz]一道题的神做法....
- 一道找规律的题
- HDU 2082 找单词
- hdu 2082 找单词
- hdu 2082 找单词
- hdu 2082 找单词
- hdu 2082 找单词
- HDU 2082 找单词
- 关于typedef的用法总结
- uCos-II操作系统在基于Cortex-M3处理器上简要移植步骤
- 物理内存与线性地址的关系
- 正则表达式学习-读书笔记
- AMD 模式介绍(en)
- hdu 2072 找单词 很不错的一道题 模拟 and STL 2种做法
- list_head内核链表的使用
- 串口屏开发流程
- 处女座——菜鸟程序员的工程总结
- Visual Leak Detector 使用
- Couldn't register with the bootstrap server错误
- ant-contrib 包扩展功能之<for> <if> <switch>
- 浅谈Python web框架
- android反编译----smali Wiki翻译--寄存器