本文共 1195 字,大约阅读时间需要 3 分钟。
为了解决这个问题,我们需要找到将字符串B插入字符串A的所有可能位置,使得插入后的字符串是一个回文串。回文串是指正读和反读都一样的字符串。
#include#include using namespace std;bool IsCircle(const string &s) { int begin = 0, end = s.size() - 1; while (begin < end) { if (s[begin] != s[end]) { return false; } begin++; end--; } return true;}int main() { string str1, str2; getline(cin, str1); getline(cin, str2); if (str2.empty()) { if (IsCircle(str1)) { cout << str1.size() + 1 << endl; } else { cout << 0 << endl; } return 0; } int count = 0; for (int i = 0; i <= str1.size(); i++) { string temp = str1; temp.insert(i, str2); if (IsCircle(temp)) { count++; } } cout << count << endl; return 0;}
转载地址:http://jpbb.baihongyu.com/