A - Antipalindrome

Problem description

A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.

A substring s[lr](1lr|s|) of a string s=s1s2s|s| is the string slsl+1sr.

Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.

Some time ago Ann read the word s. What is the word she changed it into?

Input

The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.

Output

If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.

Note that there can be multiple longest substrings that are not palindromes, but their length is unique.

Examples

Input

mew

Output

3

Input

wuffuw

Output

5

Input

qqqqqqqq

Output

0

Note

"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.

The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.

All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.

解题思路:题目的意思就是给出一个字符串,该字符串如果不是回文,则输出该字符串的长度len;是回文:如果所有字符相同,则输出0,否则输出非回文字符串的最大长度(len-1)。

AC代码一:(栈实现)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     char str[55];stack<char> s;bool flag=false;
 5     cin>>str;int len=strlen(str),t=len/2;
 6     for(int i=0;i<t;++i)s.push(str[i]);
 7     for(int i=len-t;i<len;++i){
 8         if(str[i]!=s.top()){flag=true;break;}
 9         else s.pop();
10     }
11     if(flag)cout<<len<<endl;//如果不是回文,则输出原字符串的长度
12     else{
13         int lat=len-1;
14         while(lat>0 && str[0]==str[lat])--lat;
15         if(lat!=0)cout<<len-1<<endl;//如果该字符串所有字符不都相同,则输出len-1
16         else cout<<'0'<<endl;//全部相同输出0
17     }
18     return 0;
19 }

 AC代码二:(使用C++库函数和容器set)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     string s1,s2;
 5     cin>>s1;s2=s1;
 6     set<char> r;//set容器中元素具有唯一性
 7     reverse(s1.begin(),s1.end());//反转字符串
 8     if(s1!=s2)cout<<s1.size()<<endl;//s1.length()等价于s1.size()即字符串的长度
 9     else{
10         for(unsigned int i=0;i<s1.size();++i)r.insert(s1[i]);
11         if(r.size()<2)cout<<'0'<<endl;//如果容器中只有一个元素,表明字符串中所有字符都相同
12         else cout<<s1.size()-1<<endl;
13     }
14     return 0;
15 }

 

转载于:https://www.cnblogs.com/acgoto/p/9128708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值