poj 3087 模拟题

http://poj.org/problem?id=3087

就是模拟洗牌的过程。。

学习:map<string,int >不一定要用string 用数组也可以,而且注意map的缺省值为0,也就是说如果未赋值的话就是0。

WA了好久不知道错在哪里,感觉模拟的过程没有问题,但就是错。。后来发现好像数组的大小有点问题。。第三个数组应该是200。结果开的还是100。poj居然报的不是RE。。。哭了,下次数组大小上面要小心。。干脆直接开两倍算了。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int main ()
{
	int t;
	scanf("%d",&t);
	for(int i = 1;i <= t;i++)
	{
		int n;
		char s1[110],s2[110],s[210];   //一开始没有把s 和 temp 开成两倍 orz 
		map<string,bool> mp;
		scanf("%d",&n);
		scanf("%s%s%s",s1,s2,s);
		printf("%d ",i);
		int count  = 0;
		while(1)
		{
			char temp[210];
			count++;
			for(int j = 0;j < 2*n;j++)
			{
				if(j%2==0)
				temp[j] = s2[j/2];
				else 
				temp[j] = s1[j/2];
			}
			temp[2*n] = '\0';
			if(mp[temp]==true) 
			{
				printf("-1\n");
				break;
			}
			mp[temp] = true;
			if(strcmp(temp,s)==0)
			{
				printf("%d\n",count);
				break;
			 } 
			for(int j = 0;j < n;j++)
			s1[j] = temp[j];
			s1[n] = '\0';
			int k = 0;
			for(int j = n;j < 2*n;j++)
			s2[k++] = temp[j];
			s2[n] = '\0';
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值