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 ...
随机推荐
- Windows下VS2013 C++编译测试faster-rcnn
[原创帖!转载请注明出处:http://www.cnblogs.com/LaplaceAkuir/p/6445189.html] 本人最近研究faster-rcnn,在ubuntu成功跑通matlab ...
- dataGridView 设置
//窗体加载事件 //内容居中 dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCe ...
- canvas画弧线
arc(x, y, radius, startRad, endRad, [anticlockwise]) 在Canvas画布上绘制以坐标点(x,y)为圆心.半么为radius的圆上的一段弧线.这段弧线 ...
- spring实现helloWord
第一步:添加架包 第二步:写一个简单的实列 package com.java.test; /** * @author nidegui * @create 2019-06-22 10:58 */ pub ...
- vue 导航菜单默认子路由
export default new Router({ routes: [ { path: '/', name: 'index', component: index, children: [ { pa ...
- Git server出现cache大回收分析
实例 git server是一个io密集型的服务,当cache量很大的时候,cache会全部一次释放,导致那么一瞬间,IO read压力很大,因为,用户的大量请求,需要重新从磁盘读到内存,但是这个时刻 ...
- [MySQL优化案例]系列 — 分页优化
通常,我们会采用ORDER BY LIMIT start, offset 的方式来进行分页查询.例如下面这个SQL: SELECT * FROM `t1` WHERE ftype=1 ORDER BY ...
- 使用正则表达式爬取500px上的图片
网址:https://500px.com/seanarcher,seanarcher是一个up主的名字 打开这个网址,会发现有好多图片,具体到每一个图片的url地址 https://500px.com ...
- 最小化安装CentOS-7-x86_64-Minimal-1511图文教程
说明: 虚拟机产品:VMware® Workstation 12 Pro,版本:12.5.0 build-4352439 系统镜像:CentOS-7-x86_64-Minimal-1511.iso 操 ...
- 2.3 SVN在myeclipse中的使用
一.将svn插件文件夹复制到myeclipse的dropins目录下,并重启myeclipse 二.从SVN中检查项目到myeclipse 2.打开myeclipse,点击window的show v ...