POJ3087 Shuffle'm Up

题目:

现有字符串s1、s2、s12,其中s1、s2的长度为len,s12的长度为2*len。

是否可以通过一些操作使s1和s2转换合并成s12?

变换的操作规则如下:

假设s1=11111,s2=00000

变换后的序列 s=0101010101

假设s1=12345,s2=67890

变换后的序列 s=6172839405

如果s和s12完全相同,那么输出变换次数

如果不完全相等,s的前半部分作为s1,后半部分作为s2,重复上述过程

输入:

第一行T(1≤T≤1000),代表有T组数据.

每组数据第一行len(1≤len≤100),第二行长度为len的字符串s1,第三行长度为len的字符串s2,第四行长度为2*len的字符串s12。

输出:

首先输出处理数据组的编号(编号从1开始)

再输出变换次数并换行。

注意两个数字之间有空格。

对于变换次数,如果无需变换直接得到s12,那么输出0,如果无论怎么变换都不会得到s12,那么输出 -1。

样例:

分析:简单的模拟,return-1的条件随便给了一个就水过了

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cctype>
#define PI acos(-1.0)
const int INF = 0x3f3f3f3f;
const int NINF = -INF - 1;
typedef long long ll;
using namespace std;
string a, b, c;
int n;
int func()
{
    int num = 0;
    string temp;
    temp = a + b;
    //cout << temp << endl;
    while (temp != c)
    {
        if (num == 2 * n) return -1;
        a = temp.substr(0, n);
        b = temp.substr(n, n);
        //cout << a << ' ' << b << endl;
        //break;
        int flag = 0;
        for (int i = 0; i < n; ++i)
            temp[flag++] = b[i], temp[flag++] = a[i];
        num++;
    }
    return num;
}
int main()
{
    int T, t = 0;
    cin >> T;
    while (T--)
    {
        t++;
        cin >> n;
        cin >> a >> b >> c;
        int num = func();
        cout << t << ' ' << num << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/veasky/p/10972657.html