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 ...
随机推荐
- OpenCV: Kmeans的使用一维和二维点集
OpenCVKmeans算法默认使用了Kmeans++选取种子点 参考:OpenCv中Kmeans算法实现和使用 //效果:根据半径聚类,并不一定能得到好的结果. float CBlotGlint:: ...
- 教材配套PPT初稿
1-10章初稿,基本完整.有些粗糙,后面可能会稍作调整. 附更新情况如下: 1.增加了第10章内容: 2.第5章增加了一些内容: 3.第3章内容部分更新: 4.增加了第8-9章内容. 订正:更新了第8 ...
- 在 ef 中执行 DbContext.Table.AddRange(Enitites).ToList() 会发生什么
在 ef 中执行 DbContext.Table.AddRange(Enitites).ToList() 会发生什么 昨天和朋友摸鱼,无意之间聊到了执行 DbContext.Table.AddRang ...
- 预备篇 I :范畴与函子
拓扑是研究几何图形或空间在连续改变形状后还能保持不变的一些性质的一个学科.它只考虑物体间的位置关系而不考虑它们的形状和大小. 拓扑是集合上的一种结构. 拓扑英文名是Topology,直译是地志学,最早 ...
- 点击button 触发另一个button 事件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs&quo ...
- [51Nod 1301] 集合异或和 (dp)
传送门 Solution 一道比较好的dp题 想了半天组合数QAQ 首先要知道的是 A<B一定是B有一位是1且A的这位是0且前面都相等 那么肯定是要枚举这一位在哪里然后求出方案数 方案数考虑类似 ...
- 关于linux suse11忘记root密码修改方法
SUSE Linux忘记root密码 1.重新启动机器,在出现grub引导界面后,按F2,在启动linux的选项里加上init=/bin/bash,通过给内核传递init=/bin/bash参数使 ...
- PAT 1080. Graduate Admission
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- sql语句学习(NOT EXISTS 和 NOT IN )
NOT EXISTS SELECT a.*FROM t_user aWHERE a.id_card LIKE '%3203821995100%'AND NOT EXISTS ( SELECT id F ...
- fastadmin 接口(上传)
/** * 添加注释指南 * */ public function store(Request $request) { dump($request);exit; $name = $request-&g ...