CF464A (模拟)
http://codeforces.com/contest/465/problem/C
Codeforces Round #265 (Div. 2) C
Codeforces Round #265 (Div. 1) A
http://codeforces.com/contest/464/problem/A
|
C. No to Palindromes!
time limit per test
1 second memory limit per test
256 megabytes input
standard input output
standard output Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). Sample test(s)
Input
3 3 Output
NO Input
3 4 Output
cbd Input
4 4 Output
abda Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1. The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one. A palindrome is a string that reads the same forward or reversed. |
题意:
给出一个由n个字符,由前p个小写英文字母组成的字符串,这个字符串长度不小于2的子串都不回文。求字典序大于这个的 字典序最小的符合这个条件的字符串,若没有则输出NO。
题解:模拟。
把字符串当成p位数字,+1然后进位,判断是否符合规则。
规则是不回文,考虑回文长度为2,则相邻的数字不能相同;考虑回文长度为3,则隔一个的数字不能相同;考虑更高长度的情况,发现如果没有长度为2或者3的,就没有更高长度的情况了。所以我们只用判相邻的和隔一个的等不等。
然后只这样的话final system test会跪,有数据会TLE。因为可能会加一加很多次都是回文的,我们要让这种情况一次直接加很多,不慢慢加一加一。我们发现加一进位只会改变低的若干位,回文判断只用判断这若干位,找到出现回文的最高位,如果这位不改变则再加一也会有回文,所以我们要让这位改变,最简单的就是把比它低的位全部置为最高的数字,下次加一就可以怒变了。这样就能A了。
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const double eps=1e-;
const double pi=acos(-1.0); char s[];
int n,p; int farm(){
int i,j;
int prei=n-;
while(){
j=n-;
s[j]++;///最后一位加一
while(j> && s[j]>='a'+p){///进位
s[j]-=p;
s[j-]++;
j--;
}
//printf("%d %s\n",j,s);
if(j== && s[j]>='a'+p)return ;///最高位都超上限了,爆炸
bool cant=;
int st=min(j,prei);///只观察改变了的位是否符合要求
for(i=st;i<n;i++){
if(i>= && s[i]==s[i-]){cant=;break;}
if(i>= && s[i]==s[i-]){cant=;break;}
}
if(cant){
prei=i;
for(j=i+;j<n;j++) ///第i位不符合要求,我们需要让第i位改变,
///最简单的就是把之后的为都置为最高数字,让它下次加会进位
s[j]='a'+p-;
continue;
}
return ;
}
} int main(){
scanf("%d%d ",&n,&p);
gets(s);
int ans=farm();
if(ans==)puts("NO");
else{
puts(s); }
return ;
}
CF464A (模拟)的更多相关文章
- App开发:模拟服务器数据接口 - MockApi
为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- HTML 事件(四) 模拟事件操作
本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4. ...
- 模拟AngularJS之依赖注入
一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...
- webapp应用--模拟电子书翻页效果
前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
- javascript动画系列第一篇——模拟拖拽
× 目录 [1]原理介绍 [2]代码实现 [3]代码优化[4]拖拽冲突[5]IE兼容 前面的话 从本文开始,介绍javascript动画系列.javascript本身是具有原生拖放功能的,但是由于兼容 ...
- C++ 事件驱动型银行排队模拟
最近重拾之前半途而废的C++,恰好看到了<C++ 实现银行排队服务模拟>,但是没有实验楼的会员,看不到具体的实现,正好用来作为练习. 模拟的是银行的排队叫号系统,所有顾客以先来后到的顺序在 ...
- MSYS2——Windows平台下模拟linux环境的搭建
最近从MSYS1.0迁移到了MSYS2.0,简单讲,MSYS2.0功能更强大,其环境模拟更加符合linux.虽然本身来自cygwin,但其集成了pacman软件管理工具,很有linux范,并且可以直接 ...
随机推荐
- 【BZOJ-2460&3105】元素&新Nim游戏 动态维护线性基 + 贪心
3105: [cqoi2013]新Nim游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 839 Solved: 490[Submit][Stat ...
- 策略设计模式与c语言中的函数指针
在C语言中有一个非常重要的概念-函数指针,其最重要的功能是实现回调函数(指函数先在某处注册,而它将在稍后某个需要的时候被调用)在java语言中没有指针的概念,但是可以利用接口和指针实现类似的功能,具体 ...
- 【bzoj2120】 数颜色
http://www.lydsy.com/JudgeOnline/problem.php?id=2120 (题目链接) 题意 给出一个n个数,m个询问,每次询问一个区间或修改一个数,求区间内不同的数有 ...
- uva11426 gcd、欧拉函数
题意:给出N,求所有满足i<j<=N的gcd(i,j)之和 这题去年做过一次... 设f(n)=gcd(1,n)+gcd(2,n)+......+gcd(n-1,n),那么answer=S ...
- Bzoj2563 阿狸和桃子的游戏
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 701 Solved: 496 Description 阿狸和桃子正在玩一个游戏,游戏是在一个带权图G= ...
- List<List<String>>
package list; import java.util.ArrayList; import java.util.List; public class MyList { public static ...
- PTA 链表删除结点的题目测试
链表删除结点 题目描述 输入一个正整数repeat(0 < repeat < 10),做repeat次下列运算: 输入一个正整数n(0 < n < 10)和一组( n 个 )整 ...
- Socket与SocketServer结合多线程实现多客户端与服务器通信
需求说明:实现多客户端用户登录,实现多客户端登录一般都需要使用线程技术: (1)创建服务器端线程类,run()方法中实现对一个请求的响应处理: (2)修改服务器端代码,实现循环监听状态: (3)服务器 ...
- C++ 动态数组实例
一维动态数组的实例: #include <iostream> using namespace std; int main() { int *arr; int n; cout<< ...
- Low Power Consumption Design --- MCU Attention
20161008 note : I have a PCB board called 'A' where a piece of STM8L052C6 and a piece of CC1101 are ...