codeforces C. No to Palindromes!
http://codeforces.com/contest/465/problem/C
题意:给你一个字符串,然后按照字典序找出下一个字符串,这个字符串中不能含有长度大于等于2的子串为回文串,如果含有输出,否则输出NO;
思路:判断回文串时只需判断要修改的i位置,i+1位置和i+2位置是不是与这个要修改的字符相等。字符串的前缀不含有回文子字符串,改变i位后也不会含有。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define maxn 1000010
#define ll long long
using namespace std;
const int inf=<<; int n,p;
char str[maxn];
char s1[maxn]; int main()
{
scanf("%d%d",&n,&p);
scanf("%s",str);
strcpy(s1,str);
bool flag=false;
for(int i=n-; i>=; i--)
{
for(int j=(str[i]-'a')+; j<p; j++)
{
char ch='a'+j;
if(str[max(i-,)]==ch||str[max(i-,)]==ch)
{
continue;
}
str[i]=ch;
flag=true;
break;
}
if(flag)
{
for(int j=i+; j<n; j++)
{
for(int k=; k<p; k++)
{
if((j->=&&str[j-]=='a'+k)||(j->=&&str[j-]=='a'+k))
{
continue;
}
str[j]='a'+k;
break;
}
}
if(strcmp(s1,str)!=)
{
printf("%s\n",str);
return ;
}
}
}
printf("NO\n");
return ;
}
codeforces C. No to Palindromes!的更多相关文章
- Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
- 【CodeForces】914 E. Palindromes in a Tree 点分治
[题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...
- codeforces 465C.No to Palindromes! 解题报告
题目链接:http://codeforces.com/problemset/problem/464/A 题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 ...
- codeforces 688B B. Lovely Palindromes(水题)
题目链接: B. Lovely Palindromes time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces 568a//Primes or Palindromes?// Codeforces Round #315 (Div. 1)
题意:求使pi(n)*q<=rub(n)*p成立的最大的n. 先收集所有的质数和回文数.质数好搜集.回文数奇回文就0-9的数字,然后在头尾添加一个数.在x前后加a,就是x*10+a+a*pow( ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Codeforces Round #360 (Div. 2) B. Lovely Palindromes 水题
B. Lovely Palindromes 题目连接: http://www.codeforces.com/contest/688/problem/B Description Pari has a f ...
- Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...
- CodeForces 1251B --- Binary Palindromes
[CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...
随机推荐
- JS时间戳
var timestamp1 = Date.parse(new Date()); var timestamp2 = Date.parse(new Date()); var times = timest ...
- java.io.EOFException错误
TOmcat启动后报:IOException while loading persisted sessions: Java.io.EOFException错误 - IOException while ...
- Lucene.net项目研究说明
最近项目需要全文检索,所以找了几个开源的.NET检索项目,如:Lucene.net,Sphinx,Hubble.net.最后选择使用Lucene.ne来实现全文检索.至于原因嘛,可以参考下面几点: 1 ...
- Asp.Net MVC Ajax
将ASP.NET MVC中的form提交改为ajax提交 在ASP.NET MVC视图中通过 @using (Html.BeginForm()) 产生的是form表单提交代码,可以用javascrip ...
- 关于wordpress中更换CKEditor编辑器
wordpress中自带的编辑器实在是功能太简,连插入表格都没有,使用插件的方式太过于麻烦,干脆就直接更换编辑器了,在网上找了一些方法,下文引自http://down.chinaz.com/try/2 ...
- andriod 中设置sdk升级代理服务器
Android SDK 在线更新镜像服务器资源: 大连东软信息学院镜像服务器地址: http://mirrors.neusoft.edu.cn 端口:80 北京化工大学镜像服务器地址: IPv4: h ...
- Nodejs异步流程控制Async
http://www.cnblogs.com/huair_12/p/4117351.html 很好的总结 关联下 以便以后学习使用
- Java之循环输出等腰三角形
public class aaa{ public static void main(String[] args) { int max=5; for(int i=1;i<=5;i++){//控制行 ...
- Linux C++服务器程序设计范式
<Unix网络编程>30章详细介绍了几种服务器设计范式.总结了其中的几种,记录一下: 多进程的做法: 1.每次创建一个新的请求,fork一个子进程,处理该连接的数据传输. 2.预先派生一定 ...
- 批量缩放PNG图片.
最近需要缩放N多图片, 找遍了互联网也没有找到方便使用的批量缩放工具.. 趁着周末写一个练手.. #include <iostream> #include <vector> # ...