Div2 250 - PalindromesCount
問題
2 つの文字列 A,B について A のどこかに B を挿入する.全体が回文になる挿入位置は何箇所あるか?
考え方
文字数が両方とも高々 50 文字なので全部挿入してみて回文かチェックするのが楽で速い.
コード
#include <string> using namespace std; class PalindromesCount { bool isPalindrome(string& a) { for( int i = 0, j = a.size() - 1 ; i <= j ; ++i, --j ) if( a[i] != a[j] ) return false; return true; } public: int count(string A, string B) { int ret = 0; for( int i = 0 ; i <= A.size() ; ++i ) { string a = A; a.insert(i, B); if( isPalindrome(a) ) ++ret; } return ret; } };