poj-3087

//716K  0MS G++
#include <cstdio>
#include <string>
#include <map>
#include <iostream>

using namespace std;
map<string, int> stackMap;

int stackLength;

string S1;
string S2;
string S12;
string res;

void interleave() {
    S12 = "";
    for (int i = 0; i < stackLength; i++) {
        S12 += S2[i];
        S12 += S1[i];
    }
    S1 = S12.substr(0, stackLength);
    S2 = S12.substr(stackLength, stackLength);
}

int solve() {
    stackMap.clear();
    int stepNum = 0;
    while(1) {
        interleave();
        stepNum++;
        if (S12 == res) {
            return stepNum;
        } else {
            if (stackMap.find(S12) != stackMap.end()) {
                // cout<<S12;
                return -1;
            } else {
                stackMap.insert(pair<string, int>(S12, 1));
                // cout<<" I "<<S12;
            }
        }
    }
}

int main() {
    int caseNum;
    scanf("%d", &caseNum);
    for (int i = 1; i <= caseNum; i++) {
        scanf("%d", &stackLength);
        cin>>S1;
        cin>>S2;
        cin>>res;
        printf("%d %d\n", i, solve());
    }
}


估计题类归错了,不算搜索题,每次固定只有一种选择,赢往上扯也可以认为是图。

不过考察重点应该是hash判别重复 以及字符串的操作吧。

懒得再搞了,直接STL map 和 string了, 其实是水题,不过自己2b看错了题,还有忘了清空map, 还WA了2次.

你可能感兴趣的:(poj)