poj 3087

(string) 的简单应用

#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
typedef long long ll;

const int maxn = 110;

int T, n;

map<string, bool> mp;

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	T = read();
	for(int t = 1 ; t <= T ; ++t){
		string s1, s2, res, tmp;
		cin >> n >> s1 >> s2 >> res;
		
		mp.clear();
		map<string, bool>::iterator ite;
		
		int cnt = 0;
		while(1){
			tmp = "";
			for(int i = 0 ; i < n ; ++i){
				tmp = tmp + s2[i] + s1[i];
			}
			++cnt;
			
			ite = mp.find(tmp);
			if(ite -> second == true){
				cout << t << " " << -1 << endl;
				break;
			}
			
			mp[tmp] = true;
			
			if(tmp == res){
				cout << t << " " <<cnt << endl;
				break;
			}
			
			s1 = tmp.substr(0, n);
			s2 = tmp.substr(n);
		}

	}
	return 0;
}
原文地址:https://www.cnblogs.com/tuchen/p/14822618.html