您的位置:首页 > 其它

UVA-10815 - Andy's First Dictionary

2016-07-13 16:29 501 查看

UVA-10815 - Andy’s First Dictionary

题目大意:把一篇文章中所有的单词按字典序排列

解题思路:读取单词后排序,去除重复的单词

···题目没看好,最多有5000个词但是词超过5000个,开的数组不过打Runtime-error,排序也不能用O(n^2)的,超时,最后用qsort= =过了

#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

char map[50100][210];
int cmp(const void* _a,const void* _b)
{
char* a= (char*)_a;
char* b= (char*)_b;
return strcmp(a,b);

}
bool flag[50100];
int main() {

char c,tmp[205];
int i=0,j=0;
while((c=getchar())!=EOF)
{
if(!isalpha(c))
{
if(j>0)
{
tmp[j]='\0';
strcpy(map[i],tmp);
i++;
}
j=0;
}
else
{
tmp[j]=tolower(c);
j++;
}
}
char temp[210];
int n=i-1;
qsort(map,n+1,210,cmp);

for(int i=0;i<=n;i++)
{if(strcmp(map[i],map[i+1])==0)
continue;
puts(map[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: