Description
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.
The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:
The single resultant stack, S12, contains 2 * C<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvZW0+IGNoaXBzLiBUaGUgYm90dG9tbW9zdCBjaGlwIG9mIDxzdHJvbmc+UzxzdWI+MTI8L3N1Yj48L3N0cm9uZz4gaXMgdGhlIGJvdHRvbW1vc3QgY2hpcCBmcm9tIDxzdHJvbmc+UzxzdWI+Mjwvc3ViPjwvc3Ryb25nPi4gT24gdG9wIG9mIHRoYXQgY2hpcCwgaXMgdGhlIGJvdHRvbW1vc3QKIGNoaXAgZnJvbSA8c3Ryb25nPlM8c3ViPjE8L3N1Yj48L3N0cm9uZz4uIFRoZSBpbnRlcmxlYXZpbmcgcHJvY2VzcyBjb250aW51ZXMgdGFraW5nIHRoZSAyPHN1cD5uZDwvc3VwPiBjaGlwIGZyb20gdGhlIGJvdHRvbSBvZiA8c3Ryb25nPlM8c3ViPjI8L3N1Yj48L3N0cm9uZz4gYW5kIHBsYWNpbmcgdGhhdCBvbiA8c3Ryb25nPlM8c3ViPjEyPC9zdWI+PC9zdHJvbmc+LCBmb2xsb3dlZCBieSB0aGUgMjxzdXA+bmQ8L3N1cD4gY2hpcCBmcm9tIHRoZQogYm90dG9tIG9mIDxzdHJvbmc+UzxzdWI+MTwvc3ViPjwvc3Ryb25nPiBhbmQgc28gb24gdW50aWwgdGhlIHRvcG1vc3QgY2hpcCBmcm9tIDxzdHJvbmc+UzxzdWI+MTwvc3ViPjwvc3Ryb25nPiBpcyBwbGFjZWQgb24gdG9wIG9mIDxzdHJvbmc+UzxzdWI+MTI8L3N1Yj48L3N0cm9uZz4uPC9wPgo8cD5BZnRlciB0aGUgc2h1ZmZsZSBvcGVyYXRpb24sIDxzdHJvbmc+UzxzdWI+MTI8L3N1Yj48L3N0cm9uZz4gaXMgc3BsaXQgaW50byAyIG5ldyBzdGFja3MgYnkgdGFraW5nIHRoZSBib3R0b21tb3N0IDxlbT48c3Ryb25nPkM8L3N0cm9uZz48L2VtPiBjaGlwcyBmcm9tIDxzdHJvbmc+UzxzdWI+MTI8L3N1Yj48L3N0cm9uZz4gdG8gZm9ybSBhIG5ldyA8c3Ryb25nPlM8c3ViPjE8L3N1Yj48L3N0cm9uZz4gYW5kIHRoZSB0b3Btb3N0IDxlbT48c3Ryb25nPkM8L3N0cm9uZz48L2VtPiBjaGlwcwogZnJvbSA8c3Ryb25nPlM8c3ViPjEyPC9zdWI+PC9zdHJvbmc+IHRvIGZvcm0gYSBuZXcgPHN0cm9uZz5TPHN1Yj4yPC9zdWI+PC9zdHJvbmc+LiBUaGUgc2h1ZmZsZSBvcGVyYXRpb24gbWF5IHRoZW4gYmUgcmVwZWF0ZWQgdG8gZm9ybSBhIG5ldyA8c3Ryb25nPlM8c3ViPjEyPC9zdWI+PC9zdHJvbmc+LjwvcD4KPHA+Rm9yIHRoaXMgcHJvYmxlbSwgeW91IHdpbGwgd3JpdGUgYSBwcm9ncmFtIHRvIGRldGVybWluZSBpZiBhIHBhcnRpY3VsYXIgcmVzdWx0YW50IHN0YWNrIDxzdHJvbmc+UzxzdWI+MTI8L3N1Yj48L3N0cm9uZz4gY2FuIGJlIGZvcm1lZCBieSBzaHVmZmxpbmcgdHdvIHN0YWNrcyBzb21lIG51bWJlciBvZiB0aW1lcy48L3A+Cgo8cCBjbGFzcz0="pst">Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.
Output
Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (?1) for the number of shuffle operations.
Sample Input
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1
Source
Greater New York 2006
题意: 给你a,b,c三堆牌,b放一张,a放一张,放成一堆新的牌,这样看能不能放成c,如果不能,那么底下的牌(数量已知)为a,上面为b,继续重复,
如果能放成功,输出次数,否则-1
模拟题,解释在代码
#include
#include
#include
#include
using namespace std; string ss,e,s; int ans,len; void bfs() { if(ans&&s==ss) //如果和第一次想同,不可能成功,死循环 return ; if(s==e) //放成功了 return ; ++ans; //仿牌次数增加 int i,j; string a,b; a=b=""; for(i=0;i
>len>>b>>a>>e; //不好意思,我是输入的b,a ans=0; s=""; for(i=0;i