kmjp's blog

競技プログラミング参加記です

TopCoder SRM 563 Div2 Easy FoxAndHandleEasy

つづいてDiv2もチャレンジ。
http://community.topcoder.com/stat?c=problem_statement&pm=12334

この問題、2つの文字列が与えられるので、2つ目の文字列が1つ目の文字列と、そののどこかに1つ目の文字列をもう一つ挿入した形になっているか答える問題。

最初、2つの文字列は文字単位で混ざってると思って解き方に困った。
Div2 Easyでそんな問題出すか?と。
よく見たら、挿入した側の文字列は文字単位で混ざってはいないので。

ということで、文字列を挿入したものを作って、2つ目の文字列と一致するかチェックする。

class FoxAndHandleEasy {
	public:
	char str1[256];
	char str2[256];
	
	string isPossible(string S, string T) {
		int ls,lt;
		ls=S.size();
		lt=T.size();
		if(ls*2!=lt) return string("No");
		
		int i;
		FOR(i,ls+1) {
			sprintf(str2,"%s%s",S.c_str(),S.c_str());
			memmove(str2+i,S.c_str(),ls);
			if(strcmp(str2,T.c_str())==0) return "Yes";
		}
		
		return "No";
	}

};

まとめ

問題文はちゃんと読まないとね…。