[POJ 3087] Shuffle'm Up
题目链接:http://poj.org/problem?id=3087
1 #include<cstdio> 2 #include<cstring> 3 4 using namespace std; 5 6 char ss[1000][220]; 7 char s1[110],s2[110],s12[220]; 8 int N,C; 9 10 int main() 11 { 12 scanf("%d",&N); 13 for(int flag = 1;flag <= N;flag++) 14 { 15 memset(ss,0,sizeof(ss)); 16 memset(s1,0,sizeof(s1)); 17 memset(s2,0,sizeof(s2)); 18 memset(s12,0,sizeof(s12)); 19 scanf("%d",&C); 20 scanf("%s%s%s",s1,s2,s12); 21 bool bre = false; 22 for(int i = 0;;i++) 23 { 24 for(int j=0;j<C;j++) 25 { 26 //注意合并后最底下的牌是s2的 27 ss[i][2*j] = s2[j]; 28 ss[i][2*j+1] = s1[j]; 29 } 30 if(strcmp(ss[i],s12) == 0) 31 { 32 printf("%d %d\n",flag,i+1); 33 break; 34 } 35 else 36 { 37 for(int j=0;j<i;j++) 38 if(strcmp(ss[i],ss[j]) == 0) //如果现在的顺序和之前某次顺序相同,那么肯定不会达到条件 39 { 40 printf("%d %d\n",flag,-1); 41 bre = true; 42 break; 43 } 44 if(bre) 45 break; 46 } 47 for(int k = 0;k<C;k++) 48 { 49 s1[k] = ss[i][k]; 50 s2[k] = ss[i][k + C]; 51 } 52 } 53 } 54 return 0; 55 }
用map实现,来自博客:https://blog.csdn.net/lin375691011/article/details/10114319
1 #include <stdio.h> 2 #include <map> 3 #include <string.h> 4 #include <string> 5 using namespace std; 6 int main() 7 { 8 int n,c,t=0,cut,i; 9 scanf("%d",&n); 10 while(n--) 11 { 12 scanf("%d",&c); 13 t++; 14 cut=0; 15 char s1[110],s2[110],s12[220],s[220]; 16 scanf("%s%s%s",s1,s2,s12); 17 map <string,int > map1; 18 int len,flat=0; 19 while(1) 20 { 21 cut++; 22 len=0; 23 for(i=0;i<c;i++) 24 { 25 s[len]=s2[i]; 26 len++; 27 s[len]=s1[i]; 28 len++; 29 } 30 s[len]='\0'; 31 if(strcmp(s,s12)==0) 32 { 33 flat=1; 34 break; 35 } 36 if(map1.find(s)!=map1.end()) 37 { 38 break; 39 } 40 map1[s]=0; 41 for(i=0;i<c;i++) 42 { 43 s1[i]=s[i]; 44 s2[i]=s[i+c]; 45 } 46 } 47 if(flat) 48 { 49 printf("%d %d\n",t,cut); 50 } 51 else 52 { 53 printf("%d -1\n",t); 54 } 55 } 56 return 0; 57 }