本文主要是介绍UVA10815 Andy's First Dictionary【字符串处理】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题描述
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单 词不区分大小写。
样例输入
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.
样例输出
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
解题思路:由于大小写不敏感,因此将所有的字母变为小写,将非字母的字符变成空格,然后使用strtok函数进行分割,将分割后的单词放入STL的set中,最后打印即可。注意:要选用C++11
AC的C++程序:
#include<iostream>
#include<string>
#include<stdio.h>
#include<cstring>
#include<ctype.h>
#include<set>using namespace std;
set<string>dict;void split(char *s)
{char *sp=strtok(s," ");while(sp){dict.insert(sp);sp=strtok(NULL," ");}
}const int N=10000;
char s[N];int main()
{dict.clear();while(gets(s)){for(int i=0;i<strlen(s);i++)if(isalpha(s[i]))s[i]=tolower(s[i]);elses[i]=' ';split(s);}for(set<string>::iterator it=dict.begin();it!=dict.end();it++)cout<<*it<<endl;return 0;
}
这篇关于UVA10815 Andy's First Dictionary【字符串处理】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!