题解
题目大意,给一个回文串,可以将回文串切成若干段再拼接,问最少切多少段能拼出和原有串不同的回文串
如果所给回文串除了中心位置(长度为奇数)其它都相同则无论怎么拼接出来的回文串都和原串相同,直接impossible
对于任意非第一种情况的回文串,从中心分为两部分来看,必能在一测找到切割反转后不等于另一侧的位置
则切割两次必定能拼成和原串不同的回文串(如abaacaaba,左侧ab|aa右侧aa|ba,交换得baaacaaab)
然后O(N^2)暴力找切割一次能满足条件的位置
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 5e3 + 10;
char buf[MAXN];
int main()
{
#ifdef LOCAL
//freopen("C:/input.txt", "r", stdin);
#endif
scanf("%s", buf);
string s(buf), t;
int flag = 1;
for (int i = 0; i < s.size() / 2; i++)
if (s[0] != s[i]) flag = 0;
if (flag) //所给回文除中心点全相等
cout << "Impossible" << endl, exit(0);
for (int i = 1; i < s.size(); i++)
{
t = s.substr(i) + s.substr(0, i); //暴力拼接
if (t != s)
{
int flag = 1;
for (int i = 0; i < t.size() / 2; i++)
if (t[i] != t[t.size() - i - 1])
{
flag = 0;
break;
}
if (flag)
cout << 1 << endl, exit(0); //切1次可行
}
}
cout << 2 << endl;
return 0;
}