CF1109B.Sasha and One More Name
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 k, so they got k+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 3 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 s find such minimum k, that you can cut this string into k+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 s. It there is no answer, then print "Impossible" (without quotes).
读书是萨沙的爱好之一。有一次,他在读一本书时,结识了一位不同寻常的人物。这位人物如此介绍自己:“我在各国拥有众多名字:精灵称我为‘米斯兰迪尔’,矮人唤我作‘塔库恩’,我在西方——那片已被遗忘的土地——年轻时名为‘奥罗林’;在南方,我叫‘印卡努斯’;在北方,我名‘甘道夫’;而东方,我则从不前往。”
就在那一刻,萨沙心想:那么这位人物在东方会被称作什么?在东方,所有名字都是回文串。若一个字符串正着读与倒着读完全相同,则称其为回文串。例如,“kazak”、“oo”和“r”均为回文串,而“abb”与“ij”则不是。
萨沙认为,这位英雄将以东方某位神祇之名命名。然而,由于不能存在两个相同的名字,东方人便采用如下方式:他们先将原始名字作为字符串写在一张纸上,然后以最少的切割次数 k 将这张纸切成 k+1 个片段,每个片段对应原字符串的一个子串;接着,将这些片段重新拼接成一个新的字符串(片段不可翻转,但可任意重排)。
例如,字符串 abcdefg 可由 f|de|abc|g 经过 3 次切割(交换含子串 f 和 abc 的纸片)得到;但字符串 cbadefg 则无法通过相同的切割方式获得。
更严格地讲,萨沙希望对给定的回文串 s,找出最小的 k,使得能将该字符串切割为 k+1 个部分,并将这些部分以某种顺序重新拼接,最终得到一个回文串,且该回文串不等于原始字符串 s。若不存在这样的 k,则输出 "Impossible"(不含引号)。
输入格式
The first line contains one string s (1≤∣s∣≤5000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that s is a palindrome.
第一行包含一个字符串 s(1≤∣s∣≤5000)—— 初始名称,仅由小写拉丁字母组成。保证 s 是一个回文串。
输出格式
Print one integer k — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).
输出一个整数 k —— 获得新名字所需的最少切割次数,或者输出 "Impossible"(不带引号)。
输入输出样例
输入#1
nolon
输出#1
2
输入#2
otto
输出#2
1
输入#3
qqqq
输出#3
Impossible
输入#4
kinnikkinnik
输出#4
1
说明/提示
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.
在第一个例子中,你可以在这些位置切割字符串:no|l|on,然后按如下方式重新拼接:on|l|no。可以证明,不存在仅切割一次的解。
在第二个例子中,你可以恰好在字符串中间切割,然后交换两部分,从而得到 toot。
在第三个例子中,你无法构造出一个与原字符串不相等的新字符串。
在第四个例子中,你可以将后缀 nik 切下并添加到字符串开头,从而得到 nikkinnikkin。
输入解题思路,AI测评打分。不知道怎么写?