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[l…r](1 ≤ l ≤ r ≤ |s|) of a string s = s1s2…s|s| is the string slsl + 1…sr.
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代码一:(栈实现)
#include<bits/stdc++.h>
using namespace std;
int main(){
char str[];stack<char> s;bool flag=false;
cin>>str;int len=strlen(str),t=len/;
for(int i=;i<t;++i)s.push(str[i]);
for(int i=len-t;i<len;++i){
if(str[i]!=s.top()){flag=true;break;}
else s.pop();
}
if(flag)cout<<len<<endl;//如果不是回文,则输出原字符串的长度
else{
int lat=len-;
while(lat> && str[]==str[lat])--lat;
if(lat!=)cout<<len-<<endl;//如果该字符串所有字符不都相同,则输出len-1
else cout<<''<<endl;//全部相同输出0
}
return ;
}
AC代码二:(使用C++库函数和容器set)
#include<bits/stdc++.h>
using namespace std;
int main(){
string s1,s2;
cin>>s1;s2=s1;
set<char> r;//set容器中元素具有唯一性
reverse(s1.begin(),s1.end());//反转字符串
if(s1!=s2)cout<<s1.size()<<endl;//s1.length()等价于s1.size()即字符串的长度
else{
for(unsigned int i=;i<s1.size();++i)r.insert(s1[i]);
if(r.size()<)cout<<''<<endl;//如果容器中只有一个元素,表明字符串中所有字符都相同
else cout<<s1.size()-<<endl;
}
return ;
}
最新文章
- Django(四)
- jQuery基础(1) -- jQuery 语法
- Windows 下动态链接库和静态链接库
- spring 框架通过new Object()获取applicationContext 中的bean方案
- [原创]Gerrit中文乱码问题解决方案分享
- springMVC获取file,几种转换
- 【JQGRID DOCUMENTATION】.学习笔记.2.基本表格
- XenCenter删除SR失败解决方法
- java基础知识回顾之java Thread类学习(十)--线程的状态以及转化使用的方法介绍
- VMware安装CentOS后网络设置
- 2进制,16进制,BCD,ascii,序列化对象相互转换
- IIS上不能播放mp4
- SSH-Struts(三)—ActionForm
- iOS LBS相关: 定位和中国特色的位置偏移纠正
- struts2 中文乱码问题,自定义过滤器通用解决方法
- TCP TIME WAIT
- ubuntu18.04安装mysql
- 前端和后端的数据交互(jquery ajax+python flask+mysql)
- 常用linux,DOS命令——持续更新
- redis源码学习-skiplist