timus 1711.Code Names

鼓励对该题目有过思考后再来看题解,这样会有帮助一点。

刚开始是真的彻底地没思路,后来仔细看看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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值