很笨的方法,排序然后折半。
Accepted | 1181 | C++ | 00:00.00 | 436K |
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct s
{
char w[7 ];
char sorted[7 ];
}word;
int search(word* a,int p,int r,char key[])
{
while(p <= r)
{
int q = (p + r) / 2 ;
int flag = strcmp(a[q].sorted,key);
if(flag == 0 )
return q;
else if(flag > 0 )
r = q - 1 ;
else p = q + 1 ;
}
return -1 ;
}
int cmp(const void* a, const void* b)
{
return *(char*)a - *(char* )b;
}
int cmp1(const void* a, const void* b)
{
int flag = strcmp((*(word*)a).sorted,(*(word* )b).sorted);
if(flag != 0) return flag;
else return strcmp((*(word*)a).w,(*(word* )b).w);
}
void solve(char list[][7],int cnt)
{
word w[100 ];
memset(w,0,cnt * sizeof (word));
for(int i = 0; i < cnt; i++ )
{
strcpy(w[i].sorted,list[i]);
strcpy(w[i].w,list[i]);
qsort(w[i].sorted,strlen(w[i].sorted),sizeof(char ),cmp);
}
qsort(w,cnt,sizeof (word),cmp1);
char str[7 ];
while(scanf("%s",str) != EOF && strcmp(str,"XXXXXX") != 0 )
{
qsort(str,strlen(str),sizeof(char ),cmp);
int temp = search(w,0,cnt - 1 ,str);
if(temp == -1) puts("NOT A VALID WORD" );
else
{
while(strcmp(w[temp - 1].sorted,str) == 0) temp-- ;
while(strcmp(w[temp].sorted,str) == 0 )
puts(w[temp++ ].w);
}
puts("******" );
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1181.txt","r" ,stdin);
#endif
char list[101][7 ];
int cnt = 0 ;
while(scanf("%s",list[cnt]) != EOF)
{
if(strcmp(list[cnt++],"XXXXXX") == 0 )
{
solve(list,cnt - 1 );
memset(list,0,sizeof (list));
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}
#include <string.h>
#include <stdlib.h>
typedef struct s
{
char w[7 ];
char sorted[7 ];
}word;
int search(word* a,int p,int r,char key[])
{
while(p <= r)
{
int q = (p + r) / 2 ;
int flag = strcmp(a[q].sorted,key);
if(flag == 0 )
return q;
else if(flag > 0 )
r = q - 1 ;
else p = q + 1 ;
}
return -1 ;
}
int cmp(const void* a, const void* b)
{
return *(char*)a - *(char* )b;
}
int cmp1(const void* a, const void* b)
{
int flag = strcmp((*(word*)a).sorted,(*(word* )b).sorted);
if(flag != 0) return flag;
else return strcmp((*(word*)a).w,(*(word* )b).w);
}
void solve(char list[][7],int cnt)
{
word w[100 ];
memset(w,0,cnt * sizeof (word));
for(int i = 0; i < cnt; i++ )
{
strcpy(w[i].sorted,list[i]);
strcpy(w[i].w,list[i]);
qsort(w[i].sorted,strlen(w[i].sorted),sizeof(char ),cmp);
}
qsort(w,cnt,sizeof (word),cmp1);
char str[7 ];
while(scanf("%s",str) != EOF && strcmp(str,"XXXXXX") != 0 )
{
qsort(str,strlen(str),sizeof(char ),cmp);
int temp = search(w,0,cnt - 1 ,str);
if(temp == -1) puts("NOT A VALID WORD" );
else
{
while(strcmp(w[temp - 1].sorted,str) == 0) temp-- ;
while(strcmp(w[temp].sorted,str) == 0 )
puts(w[temp++ ].w);
}
puts("******" );
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1181.txt","r" ,stdin);
#endif
char list[101][7 ];
int cnt = 0 ;
while(scanf("%s",list[cnt]) != EOF)
{
if(strcmp(list[cnt++],"XXXXXX") == 0 )
{
solve(list,cnt - 1 );
memset(list,0,sizeof (list));
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}