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 } 
时间: 2024-08-08 17:39:53

codeforce No to Palindromes!(枚举)的相关文章

codeforce Pashmak and Buses(dfs枚举)

1 /* 2 题意:n个同学,k个车, 取旅游d天! 3 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 4 5 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 6 也就是保证所有行不全相同,即每一列都是不相同的! 7 如果每一列都不相同就是表示第j个同学(第j列)在这d天中不会和其他同学(列)在这d天中 都在同一辆车中! 8 9 思路:对于每一列我们枚举d天该学生所在的车号!它的下一列只保证有一个元素和它不同就行了!依次下去! 10 11

HDU 5340 Three Palindromes( 折半枚举+Manacher+记录区间 )

Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 809    Accepted Submission(s): 240 Problem Description Can we divided a given string S into three nonempty palindromes? Input F

codeforce 589B枚举

2017-08-25 12:00:53 writer:pprp 很简单的枚举,但是我调试了很长时间,出现各种各样的问题 /* theme:cf 589B writer:pprp declare:枚举 date:2017/8/25 */ #include <bits/stdc++.h> using namespace std; const int N = 4040; typedef long long ll; ll ans = -1, record_w = -1, record_h = -1;

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbe

洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数. 编

Hdu 5340 Three Palindromes 最大回文串 Manacher

Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 80    Accepted Submission(s): 21 Problem Description Can we divided a given string S into three nonempty palindromes? Input Fir

hoj 1004 Prime Palindromes(还是不够完美)

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b

Codeforces Round #315 -Primes or Palindromes?(回文&amp;&amp;素数)

题目地址:Primes or Palindromes? 题意:给出p,q,A=p/q,求找出最大的n使得n以内的素数的个数<=A*n以内的回文串的个数. 思路:其实就是n以内的素数的个数*q<=n以内的回文串的个数*p.然而p/q的最大值为42,那么数到13000000就到达极限,所以我们直接可以从大的开始往小的一步一步的暴力枚举就可以(感觉窝还是太年轻..QAQ #include <stdio.h> #include <math.h> #include <str

Codeforce 22B Bargaining Table

B. Bargaining Table Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n?×?m meters. Each square meter of the room is either occupied by so