D. Sasha and One More Name
time limit per test 1 second memory limit per test 256 megabytes
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not."
And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not.
Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times kk, so they got k+1k+1 pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.
In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using 33 cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.
More formally, Sasha wants for the given palindrome ss find such minimum kk, that you can cut this string into k+1k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string ss. It there is no answer, then print "Impossible" (without quotes).
Input
The first line contains one string ss (1≤|s|≤50001≤|s|≤5000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that ss is a palindrome.
Output
Print one integer kk — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).
Examples
input
nolon
output
2
input
otto
output
1
input
qqqq
output
Impossible
input
kinnikkinnik
output
1
Note
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.
In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.
In the third example, you can't make a string, that won't be equal to the initial one.
In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.
大胆猜结论 爱拼才会赢
给你一个长度在5000以内的回文串,问你最少切几步可以变成与原来不同的回文串
首先因为他本身就是个回文串,在尝试之后判断答案非一即二
排除全部都是一样的字母和只有最中间一个字母不同其他完全一样的,Impossible情况
枚举所有只切一刀的可能性,构成新的串,判断是否回文且是否与原来不同
如果都不行,直接输出二
#include <bits/stdc++.h>
using namespace std;
char a[5005];
int b[30];
char c[5005];
int main()
{
scanf("%s",a+1);
int len=strlen(a+1);
int flag=0;
for(int i=1; i<=len; i++)
{
//if(a[i]=='z') cout<<i<<endl;
if(!b[a[i]-'a']) flag++;
b[a[i]-'a']++;
}
if(flag==1)
{
printf("Impossible\n");
}
else if(len%2&&b[a[1]-'a']==len-1)
{
printf("Impossible\n");
}
else
{
for(int i=1;i<=4999;i++)
{
int k=0;
flag=0;
for(int j=i+1;j<=len;j++)
{
c[++k]=a[j];
}
for(int j=1;j<=i;j++)
{
c[++k]=a[j];
}
for(int j=1;j<=len/2;j++)
{
int p=len-j+1;
if(c[j]!=c[p])
{
flag=1;
break;
}
}
int pp=0;
for(int j=1;j<=len;j++)
{
if(c[j]!=a[j])
{
pp=1;
break;
}
}
if(!flag&&pp)
{
printf("1\n");
return 0;
}
}
printf("2\n");
}
return 0;
}