您的位置:首页 > 其它

EOJ 3124. 单词表

2018-02-09 11:53 148 查看
题目链接:EOJ 3124.单词表
思路:搞清楚每个单词的截取,按字典序排序,就不难。
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define INF 501

char word[INF][50];
int s=0;

void select(char a[]) {
s=0;

int i,j,len,start;

len=strlen(a);
start=0;
for(i=0; i<len; i++) {
if(a[i]<='z'&&a[i]>='a') {
continue;
}
else if(start<i)//每个单词结束的时候要判断start的位置,防止读入分隔符
{
for(j=start; j<i; j++) {
word[s][j-start]=a[j];
}
s++;
start=i+1;
}
else
start=i+1;
}

if(start<len)
{
for(j=start; j<i; j++) {
word[s][j-start]=a[j];
}
s++;
start=i+1;
}
char tmp[INF];
for(i=0; i<s; i++) {
for(j=i+1; j<s; j++) {
if(strcmp(word[i],word[j])>0) {
strcpy(tmp,word[i]);
strcpy(word[i],word[j]);
strcpy(word[j],tmp);
}
}
}
strcpy(tmp,word[0]);
printf("%s",word[0]);

for(i=1; i<s; i++) {
if(strcmp(tmp,word[i])==0) {
continue;
} else {
strcpy(tmp,word[i]);
printf(" %s",word[i]);
}
}
printf("\n");
}

int main() {
int i,j,t;
char sentence[INF];

scanf("%d",&t);getchar();
for(i=0; i<t; i++) {

for(int h=0;h<501;h++)//清空
for(j=0;j<50;j++)
word[h][j]='\0';

gets(sentence);
printf("case #%d:\n",i);
select(sentence);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: