不复杂,只是写起来很麻烦……
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct word{
char s[7];
int len;
char* has[100];
int k;
}Word, *pWord;
void sort(char* s, int len){
int i, j;
char t;
for(i=1; i<len; i++){
t = s[i];
for(j=i-1; j>=0; j--){
if(s[j] > t)
s[j+1] = s[j];
else
break;
}
s[j+1] = t;
}
}
Word words[100];
int p;
char data[500][7];
int num;
int insert(int find, int* wh){
char st[7];
int i, k;
int len;
len = strlen(data[num]);
strcpy(st, data[num]);
sort(st, len);
for(i=0; i<p; i++){
if(words[i].len != len) continue;
for(k=0; k<words[i].len; k++){
if(st[k] != words[i].s[k])
break;
}
if(k == words[i].len){
if(find){
*wh = i;
return 1;
}
words[i].has[words[i].k++] = data[num];
return 1;
}
}
if(find) return 0;
strcpy(words[p].s, st);
words[p].len = len;
words[p].has[0] = data[num];
words[p].k = 1;
p++;
return 0;
}
void main(){
int i, j;
int wh;
char* t;
p = 0;
num = 0;
freopen("in.txt", "r", stdin);
while(scanf("%s", data[num]), data[num][0]!='X'){
insert(0, 0);
num++;
}
while(scanf("%s", data[num]), data[num][0]!='X'){
if(insert(1, &wh)){
for(i=1; i<words[wh].k; i++){
t = words[wh].has[i];
for(j=i-1; j>=0; j--){
if(strcmp(words[wh].has[j], t) > 0)
words[wh].has[j+1] = words[wh].has[j];
else
break;
}
words[wh].has[j+1] = t;
}
for(i=0; i<words[wh].k; i++)
printf("%s\n", words[wh].has[i]);
}else
printf("NOT A VALID WORD\n");
printf("******\n");
num++;
}
}