poj 3087
来源:互联网 发布:淘宝网男鞋休闲鞋2016 编辑:程序博客网 时间:2024/06/08 00:51
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 chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.
After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.
For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.
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
24AHAHHAHAHHAAAAHH3CDECDEEEDDCC
Sample Output
1 22 -1
Source
Greater New York 2006
AC代码:
#include<iostream>#include<string.h>#include<stdio.h>#include<queue>#include<algorithm>using namespace std;struct Node{ char s1[105],s2[105]; int t;};int n,r;queue <Node> qu;int trie[1000][26];int num[1000][26];int a[1000][1000]; //标记当前字符串 s1 s2是否已经访问过int insert(char *ch){ //字典树找到字符串所对应的编号 int j=0; while(*ch){ if(trie[j][*ch-'A']) j=trie[j][*ch-'A']; else{ ++r; trie[j][*ch-'A']=r; j=r; } ch++; } ch--; if(!num[j][*ch]) num[j][*ch]=++n; return num[j][*ch];}int main(){ int T; cin>>T; for(int cas=1;cas<=T;cas++){ n=r=0; memset(trie,'\0',sizeof(trie)); memset(num,0,sizeof(num)); memset(a,0,sizeof(a)); int c,sucess=0; cin>>c; char s1[105],s2[105],s3[210]; cin>>s1>>s2>>s3; cout<<cas<<' '; while(!qu.empty()) qu.pop(); Node t1; strcpy(t1.s1,s1); strcpy(t1.s2,s2); t1.t=1; qu.push(t1); a[insert(s1)][insert(s2)]=1; while(!qu.empty()){ Node tmp=qu.front(); qu.pop(); char tmp_s[210]; int i,j=0; for(i=0;i<c;i++){ tmp_s[j++]=tmp.s2[i]; tmp_s[j++]=tmp.s1[i]; } tmp_s[j]='\0'; if(strcmp(tmp_s,s3)==0){ sucess=1; cout<<tmp.t<<endl; break; } i=0; for(j=0;j<c;j++) tmp.s1[i++]=tmp_s[j]; i=0; for(;j<2*c;j++) tmp.s2[i++]=tmp_s[j]; int l=insert(tmp.s1); int r=insert(tmp.s2); if(!a[l][r]){ a[l][r]=1; tmp.t++; qu.push(tmp); } } if(!sucess) cout<<"-1"<<endl; } return 0;}
- POJ 3087
- poj 3087
- POJ 3087
- poj-3087
- poj 3087
- poj 3087
- 3087POJ
- poj--3087
- POJ 3087
- poj 3087
- POJ 3087
- POJ 3087
- POJ 3087
- poj 3087解题报告
- Poj 3087 (bfs)
- poj 3087 模拟
- poj 3087 模拟
- poj 3087 模拟题
- 现代程序员该如何学英语?
- Esper事件处理引擎_3_ObjectArray对象数组 数据结构处理
- Java中获取系统时间
- hdu poj 3216 Repairing Company floyd+最小路径覆盖
- [struts2.x] 通过Map从前端传递动态扩展的属性
- poj 3087
- Html.ActionLink几种参数格式
- pat1050
- 基于json-lib.jar包 创建JSONObject的四个常用方法
- hdu1539Shredding Company dfs
- POJ 1915 Knight Moves 双向BFS 入门
- hdu 1160 FatMouse's Speed
- 分享给有缘人——给明年依然年轻的我们:欲望、外界、标签、天才、时间、人生目标、现实、后悔、和经历
- Esper事件处理引擎_4_XML 数据结构处理