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 ;
}
A - Antipalindrome的更多相关文章
- [CSP-S模拟测试]:antipalindrome(数学)
题目传送门(内部题58) 输入格式 第一行一个数$T$表示数据组数.接下来每行两个数$n$和$m$. 输出格式 $T$行,每行一个答案,对${10}^9+7$取模. 样例 样例输入: 25 66 5 ...
- no zuo no die
#include <iostream> #include <cstring> #include <cstdio> using namespace std; name ...
- Codeforces - Avito Code Challenge 2018
Portal A. Antipalindrome 暴力. B. Businessmen Problems 暴力. C. Useful Decomposition 居然不是C打头的?! 将一棵树划分成若 ...
- Avito Code Challenge 2018 A~E
A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...
- cf掉分记——Avito Code Challenge 2018
再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...
- [CSP-S模拟测试ex]题解
爆零了.少特判见祖宗.还好这场不计入总分. 考场上什么都没想.感觉考试状态又回到了两个月前. A.Antipalindrome 手玩样例,不难发现题目中要求的合法串的充要条件是:对于任意$i \in ...
随机推荐
- 安装pywinauto的步骤
team准备搞自动化测试(桌面WPF系统),这几天一直在找自动化测试工具.发现了pywinauto这款工具,许多网友反应很好用,于是下载下来试用.不得不说遇到的坑真不少,记录下来以备不时之需. 前段时 ...
- AI:IPPR的数学表示-CNN结构/参数分析
前言:CNN迎接多类的挑战 特定类型的传统PR方法特征提取的方法是固定的,模式函数的形式是固定的,在理论上产生了特定的"局限性" 的,分类准确度可以使用PAC学习理论的方法计算出来 ...
- 图像连通域检测的2路算法Code
本文算法描述参考链接:http://blog.csdn.net/icvpr/article/details/10259577 两遍扫描法: (1)第一次扫描: 访问当前像素B(x,y),如果B(x,y ...
- 图像局部显著性—点特征(SIFT为例)
基于古老的Marr视觉理论,视觉识别和场景重建的基础即第一阶段为局部显著性探测.探测到的主要特征为直觉上可刺激底层视觉的局部显著性--特征点.特征线.特征块. SalientDetection 已经好 ...
- (转)C#开发微信门户及应用(2)--微信消息的处理和应答
http://www.cnblogs.com/wuhuacong/p/3614175.html 微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习 ...
- HTML5 video常用属性
一.视频video常用标签方法 <!-- controls 控制条,播放暂停等 controlslist 控制不允许全屏 不允许下载等 poster 封面 autoplay 自动播放 muted ...
- Tomcat jsp页面显示有问题
1.干掉tomcat下的work文件夹里面的东西,让jsp文件重新编译,相当于清楚缓存 2.work 里面是 jsp 编译的类 ,只要jsp 被访问了,就会被编译,就会生成相应的类 3.tomcat下 ...
- myeclipse中代码不显示SVN版本号
打开 : windows ->preferences->General->Appearance->Lable Decoration s 勾选其中的 SVN 项即可. 如果以上方 ...
- Python这些问题你会吗?
inal作用域的代码一定会被执行吗? 正常的情况下,finally作用域的代码一定会被执行的,不管是否发生异常.哪怕是调用了sys.exit函数,finally也是会被执行的,那怎么样才能让final ...
- lvs负载均衡net模式
环境配置,一台双网卡的ens33,ens37,ens37的网关是ens33的IP,指定一下nginx ens33,192.168.30.22,ens37,172.16.1.1nginx 192.16 ...