题意:给出几段话,要求输出这几段话中没有重复的单词,按字典序输出。
题解:根据空格和回车,将每个单词保存到数组中,先排序,然后两两比较,不同的就输出,否则忽略。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char t[1000005][205];
int cmp(const void * s1,const void * s2) {
return strcmp((char *)s1, (char *)s2);
}
int main(){
int x = 0, y = 0, i;
char c;
while((c = getchar()) != EOF) {
if (c >= 65 && c <= 90)
t[x][y++] = c + 32;
else if (c >= 97 && c <= 122)
t[x][y++] = c;
else if (y != 0) {
t[x++][y] = '\0';
y = 0;
}
}
qsort(t, x, sizeof(t[0]), cmp);
puts(t[0]);
for (i = 1;i < x; i++)
if (strcmp(t[i - 1], t[i]))
puts(t[i]);
return 0;
}