zoukankan      html  css  js  c++  java
  • POJ

    POJ - 3087链接
    这道题也是搜索?明显就是一道模拟水题嘛
    题目大意,给两堆一样长度的字符吧S1 S2 S12,开始取S2的最下面放到最下面,然后是S1的下面,依次进行。
    假定S1 = "2468", S2 = "1357" 变换后得到"12345678",如果变换后等于S12则输出操作步数,如果不是,则取变换后的前部分作为S1,后部分作为S2
    即变换后S1 = "1234",S2 = "5678",依次类推进行操作,
    我们必须明白一件事当操作数是2 * n的时候如果还没有得到S12,再次进行操作将会变回最初始的状态,这也就意味着不可能变道S12

    //Powered by CK
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    string a, b, c, temp;
    int n;
    int solve() {
        int num = 0;
        while(temp != c) {
            if(num == 2 * n)    return -1;
            temp = "";
            for(int i = 0; i < n; i++) {
                temp +=b[i];
                temp +=a[i];
            }
            a = temp.substr(0, n);
            b = temp.substr(n, n);
            num++;
        }
        return num;
    }
    int main() {
        // freopen("in.txt", "r", stdin);
        int t, x = 1;
        cin >> t;
        while(t--) {
            cin >> n >> a >> b >> c;
            int ans = solve();
            cout << x++ << " " << ans << endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    面向对象程序设计第五次作业(修改)
    C++作业 一
    面向对象程序设计第四次作业
    面向对象程序设计第三次作业
    C++学习笔记3
    C++学习笔记2
    C++学习笔记1
    面向对象程序设计作业二
    面向对象程序设计第二次作业
    随笔
  • 原文地址:https://www.cnblogs.com/lifehappy/p/12604108.html
Copyright © 2011-2022 走看看