Codeforces 464A. No to Palindromes! (dp,向前推理,向后查询)
来源:互联网 发布:webshell密码暴力破解 编辑:程序博客网 时间:2024/05/17 08:34
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 sdoesn'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.
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting ofn small English letters. It is guaranteed that the string is tolerable (according to the above definition).
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
3 3cba
NO
3 4cba
cbd
4 4abcd
abda
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.
http://codeforces.com/contest/464/problem/A
//Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;typedef long double ld;#define peter cout<<"i am peter"<<endl#define input freopen("data.txt","r",stdin)#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define gsize(a) (int)a.size()#define len(a) (int)strlen(a)#define slen(s) (int)s.length()#define pb(a) push_back(a)#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define pi 3.1415926535898#define eps 1e-6#define MOD 1000000007#define MAXN 100100#define M 200int n,p;char c,to;string row,ans;bool is_true_ok(string s,int pos){ bool ok,is_ok=true; int len=slen(s); rep(i,pos+1,len) { ok=false; for(c='a';c<=to;c++) { if(i==0 || (i>0&&c!=s[i-1])) { if(i==1 || (i>1&&c!=s[i-2])) { ok=true; s[i]=c; break; } } } if(!ok) { is_ok=false; break; } } if(!is_ok) return false; else { ans=s; return true; }}int main(){// input; string s; bool ok; cin>>n>>p; to=(p-1)+'a'; cin>>row; depin(i,n-1,0) { ok=false; for(c=row[i]+1;c<=to;c++) { if(i==0 || (i>0&&c!=row[i-1])) { if(i==0 || i==1 || (i>1&&c!=row[i-2])) { ok=true; break; } } } if(ok) { s=row; s[i]=c; if(is_true_ok(s,i)) { cout<<ans<<endl; exit(0); } } } cout<<"NO"<<endl;}
- Codeforces 464A. No to Palindromes! (dp,向前推理,向后查询)
- Codeforces 464A No to Palindromes!(构造)
- codeforces 464A No to Palindromes! 贪心
- Codeforces 464 A. No to Palindromes!
- 【codeforces】 464A No to Palindromes!
- codeforces 464A No to Palindromes! 贪心
- Codeforces 464A No to Palindromes!
- Codeforces 464A No to Palindromes!
- codeforces 464A No to Palindromes! 贪心
- CF 464A No to Palindromes!
- Codeforces Round #265 (Div. 1) A No to Palindromes!
- 【CODEFORCES】 C. No to Palindromes!
- 为查询结果建立向后/向前按钮
- Codeforces Round #265 (Div. 2) C.No to Palindromes!
- Codeforces Round #265 (Div. 2) C. No to Palindromes!
- Codeforces Round #265(div2) C. No to Palindromes!
- Codeforces Round #265 (Div. 2) C. No to Palindromes!
- Codeforces Round #265 (Div. 2) C - No to Palindromes!
- 避免过度同步(笔记)
- Spring中Quartz的配置
- Java正则中的Greedy、Reluctant 、Possessive qualifier
- 两个栈实现队列 && 两个队列实现栈
- 阿里巴巴2015年秋季在线笔试附加题---求交集字符串
- Codeforces 464A. No to Palindromes! (dp,向前推理,向后查询)
- 编译原理实验之源程序的预处理及词法分析程序设计
- 2014年暑假总结
- hdu 2030
- 正则基础之——贪婪与非贪婪模式
- 例题7-7 天平难题 UVa1354
- Codeforces 464B. Restore Cube (一种高效八点判断立方体(cube)的办法)
- iOS UI核心
- RocketMQ原理解析-producer 2.如何发送消息