没有什么花头,跟之前做的一道题几乎是用同样的解法。(详见Language of FatMouse)
Accepted
|
C
|
00:00.00
|
404K
|
#include<stdio.h>
#include<string .h>
#include <stdlib.h>
#define swap(a,b) {a = a ^ b; b = b ^ a; a = a ^ b;}
char dic[101][21 ];
int cmp(const void* a,const void* b)
{
return strcmp((char*)a,(char* )b);
}
void solve()
{
int cnt1,cnt2,cnt3 = 0 ,i,j;
char w[21],res[101][21 ];
scanf("%d",& cnt1);
for(i = 0; i < cnt1; i++ )
scanf("%s",& dic[i]);
qsort(dic,cnt1,sizeof(dic[0 ]),cmp);
scanf("%d",& cnt2);
for(i = 0; i < cnt2; i++ )
{
scanf("%s",& w);
int len = strlen(w);
char *t = (char*)bsearch(w,dic,cnt1,sizeof(dic[0 ]),cmp);
if(t != NULL)
{
puts(w);
continue ;
}
cnt3 = 0 ;
for(j = 0; j < len - 1; j++ )
{
swap(w[j],w[j + 1 ]);
t = (char*)bsearch(w,dic,cnt1,sizeof(dic[0 ]),cmp);
if(t != NULL)
strcpy(res[cnt3++ ],t);
swap(w[j],w[j + 1 ]);
}
if(cnt3 == 0 )
puts(w);
else
{
qsort(res,cnt3, sizeof(res[0 ]),cmp);
for(j = 0; j < cnt3; j++ )
j != 0 ? printf(",%s",res[j]) : printf("%s" ,res[j]);
putchar(10 );
}
}
}
void main()
{
int t;
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#include<string .h>
#include <stdlib.h>
#define swap(a,b) {a = a ^ b; b = b ^ a; a = a ^ b;}
char dic[101][21 ];
int cmp(const void* a,const void* b)
{
return strcmp((char*)a,(char* )b);
}
void solve()
{
int cnt1,cnt2,cnt3 = 0 ,i,j;
char w[21],res[101][21 ];
scanf("%d",& cnt1);
for(i = 0; i < cnt1; i++ )
scanf("%s",& dic[i]);
qsort(dic,cnt1,sizeof(dic[0 ]),cmp);
scanf("%d",& cnt2);
for(i = 0; i < cnt2; i++ )
{
scanf("%s",& w);
int len = strlen(w);
char *t = (char*)bsearch(w,dic,cnt1,sizeof(dic[0 ]),cmp);
if(t != NULL)
{
puts(w);
continue ;
}
cnt3 = 0 ;
for(j = 0; j < len - 1; j++ )
{
swap(w[j],w[j + 1 ]);
t = (char*)bsearch(w,dic,cnt1,sizeof(dic[0 ]),cmp);
if(t != NULL)
strcpy(res[cnt3++ ],t);
swap(w[j],w[j + 1 ]);
}
if(cnt3 == 0 )
puts(w);
else
{
qsort(res,cnt3, sizeof(res[0 ]),cmp);
for(j = 0; j < cnt3; j++ )
j != 0 ? printf(",%s",res[j]) : printf("%s" ,res[j]);
putchar(10 );
}
}
}
void main()
{
int t;
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
while(scanf("%d",&t) != EOF)
while(t-- )
{
solve();
if(t != 0 )
putchar(10 );
}
#ifndef ONLINE_JUDGE
fclose(stdin);
while(scanf("%d",&t) != EOF)
while(t-- )
{
solve();
if(t != 0 )
putchar(10 );
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}
}