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 ...
随机推荐
- 在String中添加移动构造函数和移动赋值运算符
13.50 没有定义析构函数 #include<iostream> #include<string> #include<memory> #include<ut ...
- labview 移位寄存器、隧道、索引隧道的区别
Lab view区别:移位寄存器.隧道.索引隧道 最近研究Lab view的时候发现移位寄存器和隧道的功能非常相似但是又有区别 外部数据进入循环体是通过隧道进入的,有几种方式: 图1:For 循环结构 ...
- hdu2041java
超级楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Android(java)学习笔记184:生成 4种 不同权限的文件
1.首先我们编写一个生成 4种 不同权限的文件的程序案例: (1)首先是activity_main.xml文件: <RelativeLayout xmlns:android="http ...
- Jquery调用从ashx文件返回的jsonp格式的数据处理实例
开发环境:vs2010+jquery-1.4.min.js 解决问题:网上代码比较少,好多调试不通,返回数据不用json而用jsonp主要考虑解决跨域问题 开发步骤:打开VS2010,新建一web站点 ...
- Linux平台的boost安装全解
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- CentOS 6.7 配置nginx支持SSL/https访问
一.安装必要的包 yum install openssl openssl-devel 二.配置编译参数,增加对SSL的支持 ./configure –with-http_ssl_module 三.修改 ...
- jQuery Ajax 实例 全解析
jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 废话少说,直接进入正题,我们先来看一些简单的方法,这些方法都是 ...
- Javascript基础学习(2)_表达式和运算符
1.==和===的区别(!=和!==是相反的比较) 它们采用了同一性的两个不同定义.==是相等性,===是等同性. ①“===”进行两个值的比较 两个值的类型不同,就不相等 两个值是数字,并且值相同, ...
- ubuntu出现有线已连接却无法上网
或者直接追加到/etc/sysctl.conf 如果遇到“设备未托管”,一般是台式机默认移动ip后禁用网络. 那么修改/etc/NetworkManager/NetworkManager.conf,设 ...