Uva--10815 (字符串处理)
2014-05-31 19:38:36
题意 & 思路:以scanf(%c)的方式吃单词(虽然getline也可以,但scanf %c不容易出错),因为要输出不同的单词,而文章中的单词可能重复,那就空间换时间。
#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <string.h> using namespace std; struct Word{ char s[50]; }; Word word[500000]; bool cmp(Word a,Word b){ return strcmp(a.s,b.s) < 0; } void Lowcase(char *p,int len){ for(int i = 0; i < len; i++){ if(p[i] >= 'A' && p[i] <= 'Z'){ p[i] += ('a' - 'A'); } } } int main(){ //freopen("in","r",stdin); int num = 0,cnt = 0,flag = 0; char sen; while(scanf("%c",&sen) == 1){ if((sen >= 'A' && sen <= 'Z') || (sen >= 'a' && sen <= 'z')){ word[num].s[cnt++] = sen; flag = 1; } //隐含着不为字母的意思 else if(flag){ word[num].s[cnt] = '\0';//单词结尾 Lowcase(word[num].s,cnt); flag = 0; cnt = 0; ++num;//记录数下移 } } sort(word,word + num,cmp); cout << word[0].s << endl; for(int i = 1; i < num; i++){ if(strcmp(word[i].s,word[i - 1].s) != 0) cout << word[i].s << endl; } return 0; }