codeforce No to Palindromes!(枚举)
生活随笔
收集整理的這篇文章主要介紹了
codeforce No to Palindromes!(枚举)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 /*
2 題意:給定一個字符串中沒有任何長度>1的回文子串!求按照字典序的該串的下一個字符串
3 也不包含長度>1的任何回文子串!
4
5 思路:從最低位進行枚舉,保證第i位 不與 第 i-1位和第 i-2位相同就好了!那么因為前邊i-1
6 位沒有長度>1的回文子串,那么前i位也不會出現!最后將最后邊的字符按照相同的原則補齊就好了!
7 */
8 #include<iostream>
9 #include<cstdio>
10 #include<cstring>
11 #include<algorithm>
12
13 using namespace std;
14
15
16 char ch[1005];
17
18 int main(){
19 int n, p;
20 while(scanf("%d%d", &n, &p)!=EOF){
21 ch[0]='0';
22 ch[1]='0';
23 int up='a'+p-1;
24 scanf("%s", ch+2);//從字符串第二位輸入是因為第一位,第二位也要枚舉
25 bool flag=false;
26 for(int i=++n; i>=2 && !flag; --i){//枚舉每一位
27 for(int j=ch[i]+1; j<=up && !flag; ++j){//每一位應該出現的字符(從小到大)
28 ch[i]=j;
29 if(ch[i]!=ch[i-1] && ch[i]!=ch[i-2]){//保證三者互不相同
30 flag=true;
31 for(int k=i+1; k<=n; ++k){//補全后面
32 int cc;
33 for(cc='a'; cc<=up; ++cc)
34 if(cc!=ch[k-1] && cc!=ch[k-2]) break;
35 ch[k]=cc;
36 }
37 }
38 }
39 }
40 if(flag) printf("%s\n", ch+2);
41 else printf("NO\n");
42 }
43 return 0;
44 }
?
轉載于:https://www.cnblogs.com/hujunzheng/p/3964726.html
總結
以上是生活随笔為你收集整理的codeforce No to Palindromes!(枚举)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bios怎么找从光盘启动 如何设置bio
- 下一篇: 怎么让电脑开机启动u盘 电脑如何通过U盘