字符串模擬,枚舉自己和對手的牌的全排列即可
[cpp]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[10][10];
char str[10][25];
int a[10],b[10];
int n,flag;
int check(int a[10],int b[10])
{
int num1=1,num2=1;
while(num1<=n&&num2<=n)
{
if(map[b[num2]][a[num1]])
num1++;
else
num2++;
}
if(num2>num1)
return 1;
return 0;
}
int main()
{
int k,t,i,tt,j;
char s[25];
scanf("%d",&t);
for(k=1;k<=t;k++)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%s",str[i]);
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
scanf("%d",&tt);
while(tt--)
{
scanf("%s",s);
for(j=1;j<=n;j++)
if(strcmp(s,str[j])==0)
{
map[i][j]=1;
break;
}
}
}
for(i=1;i<=n;i++)
a[i]=i;
do
{
flag=1;
for(i=1;i<=n;i++)
b[i]=i;
do
{
if(!check(a,b))
{
flag=0;
break;
}
}while(next_permutation(b+1,b+n+1));
if(flag)
break;
}while(next_permutation(a+1,a+n+1));
printf("Case %d: ",k);
if(flag==0)
printf("No\n");
else
{
printf("Yes\n");
for(i=1;i<=n;i++)
{
if(i==n)
printf("%s\n",str[a[i]]);
else
printf("%s ",str[a[i]]);
}
}
}
return 0;
}
作者:Cambridgeacm