zoukankan      html  css  js  c++  java
  • A

    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 }
  • 相关阅读:
    JS面向对象编程的实现
    初见Javascript
    详解promise
    radio单选按钮组操作
    cookie欺骗实战案例
    XSS攻击
    前端如何实现异步加载
    日常问题
    求1+2+...+n
    二叉搜索树的后序遍历序列
  • 原文地址:https://www.cnblogs.com/acgoto/p/9128708.html
Copyright © 2011-2022 走看看