题目链接

题意:有两个字符串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=, n, ans, j;
scanf("%d", &T);
while(T--)
{
char s1[N]={}, s2[N]={}, s[N]={}, str[N]={};//记得初始化;
map<string,int>maps;//个人理解为字符串的值;
int flag = ;
scanf("%d", &n);
scanf("%s%s%s", s1, s2, s);
ans = ;
while()
{
ans++;
j=;
for(int i=; i<n; i++)
{
str[j++] = s2[i];
str[j++] = s1[i];
}
s[j]='\0';
if(strcmp(str, s)==)break;
if(maps[str]==){flag=;break;}//导致循环;
maps[str]=;//存在过; strncpy(s1, str, n);
strncpy(s2,str+n, n);//更新s1,s2;
}
if(flag==)
printf("%d -1\n", t++);
else
printf("%d %d\n", t++, ans);
}
return ;
}
05-27 05:35