CodeForces - 1113 D Sasha and One More Name【字符串】

Sasha在阅读中遇到一个自称在东方有着回文名字的角色。为了理解这个角色在东方可能的名字,Sasha需要通过最少次数的切割,将原始名字重组为新的回文名字,且新名字不能与原名相同。此过程涉及字符串操作和回文判断,旨在找到最优的切割次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Sasha and One More Name

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 fand 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

Copy

nolon

output

Copy

2

input

Copy

otto

output

Copy

1

input

Copy

qqqq

output

Copy

Impossible

input

Copy

kinnikkinnik

output

Copy

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.

 

 

因为S为回文串,所以取S的前缀t1和后缀t2,当t1!=t2时,交换他们即可满足题意,那么最终答案只可能是1,2和impossible

(1)当s[0]到s[n/2]全部相等时,答案是impossible

(2)当n为奇数时,不存在答案为1的情况

         当n为偶数时,将s对半分,如果两个子串不相等,即子串不回文,即答案为1。若子串相等且n/2为偶数,继续对半分,直到出现不回文的子串。

 

#include <bits/stdc++.h>
using namespace std;
string s;
bool che(int n)//暴力判断是否回文
{
    for (int i = 0; i < (n)/2; ++i) {
        if(s[i]!=s[n-i-1])return 0;
    }
    return 1;
}
bool che1()
{
    int len = s.length();
    if(len&1)return 0;//不能为奇数
    while(len)
    {
        len/=2;
        if(!che(len))
        {
            return 1;
        }
        else if(len&1) return 0;
    }
    return 0;
}
bool cheim()
{
    int n=s.length();
    for (int i = 0; i < (n)/2-1; ++i) {
        if(s[i]!=s[i+1])return 0;
    }
    return 1;
}
bool che2()
{
    int n=s.length();
    for (int i = 0; i < (n-1)/2; ++i) {
        if(s[i]!=s[s.length()-i])return 1;
    }
    return 0;
}
int main()
{
    cin>>s;
    if(cheim())puts("Impossible");
    else if(che1())puts("1");
    else if(che2())puts("2");
    else puts("Impossible");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值