Winter-2-STL-E Andy's First Dictionary 解题报告及测试数据
use stringstream
Time Limit:3000MS Memory Limit:0KB
Description
Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.
You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.
Input
The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.
Output
Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.
Sample Input
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left."
So they went home.
Sample Output
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
题解:
这道题最重要的就是stringstream的使用。
使用getline(cin,string)函数整行读取。之所以不直接读取,是因为两个单词间有标点符号时,会误读为一个单词,还是需要后续的处理。
由于最后要求输出的是小写,也方便排序,那么每输入一行就进行标点变为空格,大写变为小写的变换。处理完之后,就需要用到stringstream(字符串流)类,头文件是<sstream>,例如stringstream ss,ss.clear()是将字符串流清空,ss.str(string)是将ss读入对象初始化为形参 string,那么执行ss>>str,那么执行
while
(ss>>str[k])k++;
就可以将不带空格的字符串一一读入,从而实现了单词之间的分离。排序之后,使用pre记录刚刚输出的字符串,如果当前字符串与pre相同,则是重复的,不输出。因为排序后相同的字符串是连在一块的,所以用这种方法就将重复的全部排除了。
以下是代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <sstream>
using namespace std;
string str[100000],s;
stringstream ss;
int k=0;
int main(){
//freopen("1.in","r",stdin);
while(getline(cin,s)){
int len = s.length();
for(int i=0;i<len;i++){//将标点变为空格,大写变为小写。
char ch = s.at(i);
if(islower(ch))continue;
if(isupper(ch))s.at(i)-='A'-'a';
else s.at(i)=' ';
}
ss.clear();
ss.str(s);
while(ss>>str[k])k++;
}
sort(str,str+k);//排序
string pre="";
for(int i=0;i<k;i++){
if(str[i]==pre)continue;//重复的不输出
cout << str[i]<<endl;
pre =str[i];
}
}
Winter-2-STL-E Andy's First Dictionary 解题报告及测试数据的更多相关文章
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...
- LeetCode 953 Verifying an Alien Dictionary 解题报告
题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...
- 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- UVa 10815 Andy's First Dictionary
感觉这道题要比之前几个字符串处理的题目难度要大了一些. 题目大意:给若干行字符串,提取出所有单词并去掉重复的,最后按字典顺序输出. 对于输入大致有两种思路,一种是逐个读入字符,遇到字母的话就放到wor ...
- UVa10815.Andy's First Dictionary
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Problem C: Andy's First Dictionary
Problem C: Andy’s First DictionaryTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 5[Submit] ...
- 【UVA - 10815】Andy's First Dictionary (set)
Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英 ...
- UVA-10815 Andy's First Dictionary (非原创)
10815 - Andy's First Dictionary Time limit: 3.000 seconds Problem B: Andy's First DictionaryTime lim ...
随机推荐
- VC++ Splash Window封装类CSplash
Splash.h 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 ...
- Administrator privileges required for OLE Remote Procedure Call debugging: this feature will not work..
VC++ 6.0单步调试(F10)出现警告框: 解决方法: 右键VC++ 6.0程序图标
- JQuery------实现点击左右按钮,切换图片功能
如图: 代码: html @*商品主图片*@ <div class="product-img" style="display:table-cell;width:40 ...
- 编程之美 set 11 买书问题
题目 书店搞促销, 同时购买多卷书时, 有机会享受优惠 2本优惠 5%, 3本 10%, 4 本 20% 5 本 25% 设计算法, 求解购买一本书的最低价格 分析 1. 第一个感觉是一次购买的越多省 ...
- ios 开发之 -- 极光推送,发送自定义消息,进入制定页面
在进行极光推送时候,发现版本有所更新,以前截取didfinish入口方法里面的launchOptions,获取一个本地的通知内容,进行本地展示不可用了,通过查询官方文档和网上的资料才发现,方法改变了, ...
- 让iOS应用支持不同版本的系统与设备
本文转载至 http://blog.csdn.net/pucker/article/details/11980811 最近一直在做app的iOS 6和7的同时适配工作,所以在此介绍一下系统与设备的兼 ...
- Ubuntu16.04安装QQ(图文说明)
导读 最近,因为工作需要,我安装了Ubuntu16.04,然而有好多不便,工作上的事情大多需要QQ联系,然而在Ubuntu上的WebQQ很是不好用,于是在网上搜索了好多个linux版本的QQ,然而不是 ...
- HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
- win10系统安装Oracle11报错不满足最低要求
以下问题是,在安装Oracle过程中遇到的问题以及自己的解决方法: 问题一:下载好两个Oracle的安装压缩包文件,同时解压后,启动setup.exe,报错:[INS-13001]环境不满足最低要求. ...
- 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则
负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...