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 ...
随机推荐
- 利用jsonp进行Ajax跨域请求
在进行Ajax请求的时候经常会遇到跨域的问题,这个时候一般就会用到jsonp. 关于json和jsonp,网上有很多原理解释,这里就不多赘述,需要的自行搜索. 下面是一个简单的ajax跨域请求示例: ...
- sqlitManager
@interface sqlitManager : NSObject +(instancetype)sharedSqlitManager; -(void)createDB; -(void)create ...
- Django逻辑关系
title: Django学习笔记 subtitle: 1. Django逻辑关系 date: 2018-12-14 10:17:28 --- Django逻辑关系 本文档主要基于Django2.2官 ...
- day34-3 类和对象小知识
目录 属性查找顺序 类与对象的绑定方法 类与数据类型 对象的高度整合 属性查找顺序 属性查找顺序:先从对象自身查找,对象没有就去类中查找,类中没有则报错 class Student: name = ' ...
- vue组件的拆分
vue组件的拆分 <div class="div"> <!-- 拆分出来的组件 自定义名字moban --> <moban></moban ...
- NOIP2012 DAY2 T2借教室
题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然 ...
- Linux思维导图之rpm、yum、编译
yum安装失败: 1.yum client 路径指向不正确:2.yum server 缓存未清理(yum clean all;yum makecache):3.网络不连通
- mongodb集群搭建(分片+副本)
原文地址:https://www.cnblogs.com/ityouknow/p/7344005.html 相关概念 先来看一张图: 从图中可以看到有四个组件:mongos.config server ...
- js中三个默认方法call,applay,bind
这三个都是函数自带的方法(Function.prototype),这三个方法都能够改变函数内部 this的指向, call //call方法接收三个参数,第一个是this指向,第二个,三个是传递给函数 ...
- EasyUI Messager 消息框点击右上角x无法执行回掉函数解决
我先声明下,我是改动源码,网上的其他方法我试过了,不行.我是写java后台的,前端也是小白,可能讲的也也不是很清楚. 首先我想到的是在网上找到解决方法,我找到一个方法说在jquery.easyui.m ...