题目网址: POJ -- 3087ios
这道题目是个模拟题,题目比较长,特别是哪是牌底哪是牌顶,分割的时候谁作S1谁作S2要分清楚。数组
惟一用到的就是判重,我是用STL里面的set去判重,只用到insert()和count()两个成员函数就ok。函数
其实不太懂这道题目的数据规模,牌堆高度给的最高是100,相互洗牌产生的牌堆按说应该特别多种才对,由于我见题目上没有给牌的颜色加限制,若是每张牌颜色都不一样,恐怕组合数会特别多。可是这道题目应该没有给那么变态的数据,直接模拟就给过了。spa
#include <cstdio> #include <cstring> #include <set> #include <string> #include <iostream> using namespace std; const int maxn = 110*2; int main() { int N; cin >> N; int kase = 0; char buf[maxn]; while(N--) { cout << ++kase << " "; string S1, S2, R, A; int C, ans = 0; cin >> C >> S1 >> S2 >> R; set<string> Set; for(;;) { for(int i = 0; i < 2*C; i++) { if(i % 2) buf[i] = S1[i/2]; else buf[i] = S2[i/2]; } buf[2*C] = '\0'; A = (string)buf; ans++; if(A == R) { cout << ans << endl; break; } if(Set.count(A)) { cout << -1 << endl; break; } Set.insert(A); S1.clear(); for(int i = 0; i < C; i++) S1.push_back(A[i]); S2.clear(); for(int i = C; i < 2*C; i++) S2.push_back(A[i]); } } return 0; }
还有一个字符数组强转string类的用法,处理字符串的时候挺好用的。毕竟要用到set判重,构建set<string>是确定的。code