题意:有两个字符串s1,s2;经过交叉问是否得到字符串s,不能输出-1,能就输出交叉的次数
每次重组的串都是s2开始,重新组合时,前面一半是s1,后一半s2;
#include<stdio.h> #include<vector> #include<iostream> #include<string.h> #include<algorithm> #include<map> #define N 250 using namespace std; int main() { int T, t=1, n, ans, j; scanf("%d", &T); while(T--) { char s1[N]={0}, s2[N]={0}, s[N]={0}, str[N]={0};//记得初始化; map<string,int>maps;//个人理解为字符串的值; int flag = 0; scanf("%d", &n); scanf("%s%s%s", s1, s2, s); ans = 0; while(1) { ans++; j=0; for(int i=0; i<n; i++) { str[j++] = s2[i]; str[j++] = s1[i]; } s[j]=‘\0‘; if(strcmp(str, s)==0)break; if(maps[str]==1){flag=1;break;}//导致循环; maps[str]=1;//存在过; strncpy(s1, str, n); strncpy(s2,str+n, n);//更新s1,s2; } if(flag==1) printf("%d -1\n", t++); else printf("%d %d\n", t++, ans); } return 0; }
Shuffle'm Up---poj3087
时间: 2024-11-10 00:52:38