鼓励对该题目有过思考后再来看题解,这样会有帮助一点。
刚开始是真的彻底地没思路,后来仔细看看test1的output:
codenames grille keywords mnemonic playgame random rectangle rejudge shaitan volume watchmen
output呈字典序排列,才清楚题意,有就知道为什么test2的结果会是IMPOSSBLE了
其中input为
11 cipher grille kamkohob names codenames codes newtests rejudge timus size volume summit watchmen braineater twosides solution random yesorno keywords subversion commands bosses shooting shaitan game strategy playgame mnemonic palindromes bestname eligibility rectangle rules 2 1 7 10 9 6 11 3 8 4 5
实现的代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct A //数据结构
{
int num;
char s[25];
};
A a[50];
int cmp(A aa,A b)//字典序排列
{
int i;
for(i=0;;)
{
if(aa.s[i]>b.s[i])return 0;
else if(aa.s[i]<b.s[i])return 1;
i++;
}
return 1;
};
int main()
{
int n,i,j,arr[20];
char ans[20][25]; //记录最容易找的方案
while(scanf("%d",&n)!=-1)
{
for(i=0;i<3*n;i++)
{
scanf("%s",a[i].s);
a[i].num=i/3+1;
}
sort(a,a+3*n,cmp);
for(i=1;i<=n;i++)
scanf("%d",&arr[i]);
int kk=0,i=1,ff=0; //ff用来判断是否有满足条件的解法,kk记录上个arr[]访问的位置
while(kk<3*n)
{
for(j=kk;j<3*n;j++)
{
if(a[j].num==arr[i])
{
strcpy(ans[i],a[j].s);
i++;
break;
}
}
kk=j+1;
if(i==n+1)
{
ff=1;
break;
}
}
if(ff)
{
for(i=1;i<=n;i++)
{
printf("%s\n",ans[i]);
}
}
else
{
printf("IMPOSSIBLE\n");
}
}
return 0;
}