题意:两堆个数相等的牌,洗牌,右手边的先下一张,左手边的再下一张,依次洗完一次牌,洗完之后从底往上数C个为左手牌,剩下的是右手牌,然后可以洗很多次,直到洗到题中要求的顺序,若是洗不出题中要求的顺序输出-1。
思想:用map判重,遇到重复的直接输出-1 ,因为这会产生循环,没必要继续下去。
#include<iostream> #include<cstring> #include<cstdio> #include<map> using namespace std; int C; string c1,c2,c; int Get_Ans(){ int strmap_id = 0; int temps = 0; map<string,int>strmap; strmap.clear(); while(++temps){ string str = c1 + c2; int c1_pt = 0,c2_pt = 0; //cout<<c1<<" "<<c2<<" "; for(int i = 0; i <= 2 * C - 1; ++i){ if(i % 2){ str[i] = c1[c1_pt++]; } else{ str[i] = c2[c2_pt++]; } } //cout<<str<<endl; if(strmap[str]) return -1; if(str == c) return temps; strmap[str] = strmap_id++; c1 = str.substr(0,C); c2 = str.substr(C,C); } } int main() { int N,ca = 1; scanf("%d",&N); while(N--){ scanf("%d",&C); cin>>c1; cin>>c2; cin>>c; int ans = Get_Ans(); cout<<ca++<<" "<<ans<<endl; } return 0; }