【链接】 我是链接,点我呀:)

【题意】

题意

【题解】

因为原序列没有任何长度超过2的回文串。
所以,我们在改变的时候,只要时刻保证改变位置s[i]和s[i-1]以及s[i-2]都不相同就好。
因为只改变一个位置的话是不会产生长度超过3的回文串的
我们按照从后到前,从小到大的顺序,尝试增加第i位的字符就好。
只要找到一个位置idx可以增加一下,那么后面我们只要按照之前的规则,
让idx+1~len依次让每一位从'a'开始到'a'+p-1进行枚举构造出来一个最小字典序的符合要求的字符串就好.
(如果idx+1~len不能构造出来,那么会发现a[idx]不论改成什么都还是一样的,因为我们在考虑的时候,肯定会满足a[idx-1]和a[idx]不同,而后面也是如此
所以a[idx]是什么对于后面来说已经不重要了,没有影响,所以不用担心会漏解)
(main函数记得加上Public。。。)

【代码】

import java.util.*;

public class Main{

	static int n,p;
static int a[] = new int[1010]; static boolean dfs(int dep) {
if (dep==0) return false;
for (int i = a[dep]+1;i <= p;i++) {
if (dep-1>=1 && a[dep-1]==i) continue;
if (dep-2>=1 && a[dep-2]==i) continue;
a[dep] = i;
return true;
}
if (!dfs(dep-1)) return false;
a[dep] = 1;
for (int i = a[dep];i<=p;i++) {
if (dep-1>=1 && a[dep-1]==i) continue;
if (dep-2>=1 && a[dep-2]==i) continue;
a[dep] = i;
return true;
}
return false;
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s;
n = in.nextInt();p = in.nextInt();
s = in.next();
for (int i = 1;i <= n;i++) {
a[i] = (int)(s.charAt(i-1)-'a'+1);
}
if (dfs(n))
for (int i = 1;i <= n;i++)
System.out.print((char)(a[i]+'a'-1));
else
System.out.println("NO");
}
}

【Codeforces 464A】No to Palindromes!的更多相关文章

  1. 【34.88%】【codeforces 569C】Primes or Palindromes?

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【25.64%】【codeforces 570E】Pig and Palindromes

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. Hibernate的表之间的关系

    表:A.B 一对多关系:A中的一个字段的记录(主键)引用B中的多个记录(外键) 多对一关系:A中的一个字段的记录(外键)引用B中的一个记录(主键) 一对一关系:A.B两个表的关联字段都是主键 多对多关 ...

  2. P3174 [HAOI2009]毛毛虫(树形dp)

    P3174 [HAOI2009]毛毛虫 题目描述 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一个毛毛虫,点数越多,毛毛虫就越大.例如下图左边的树(图 1 )抽出一部分就变成了右边 ...

  3. 2017杭电多校第七场1011Kolakoski

    Kolakoski Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Tota ...

  4. 用SpringMVC实现的上传下载方式二(多文件上传)

    参考来源:      http://blog.csdn.net/qq_32953079/article/details/52290208 1.导入相关jar包 commons-fileupload.j ...

  5. MySQL的主从复制(windows)

    在我们实际的开发中,当系统业务到达一定的程度,可能数据库会到达一定的瓶颈,但实际开发中最容易到达数据库瓶颈的应该是数据库的读性能,一般的业务大多都是读多写少,我们可以通过提高读的性能来提高数据库的整体 ...

  6. TypeError: string indices must be integers, not str

    1. TypeError: string indices must be integers, not str 字符串类型取第index个字符的时候,应该传入int而不是str.如 1 a='abcde ...

  7. ES6:Generator函数(1)

    Generator函数是ES6提供的一种异步编程解决方案.它会返回一个遍历器对象 function* helloWorldGenerator(){ yield “hello”; yield “worl ...

  8. express模块安装使用命令配置

    之前的博客nodejs安装和配置好路径之后就可以安装express了: 随便打开个文件夹右键选择,git bash here 命令行里输入[npm install express -g] -g是全局安 ...

  9. iOS设计模式——Category和 Extension

    什么是Category Category模式用于向已经存在的类添加方法从而达到扩展已有类的目的,在很多情形下Category也是比创建子类更优的选择.新添加的方法同样也会被被扩展的类的所有子类自动继承 ...

  10. Memcached通信协议

    英文水平很烂,做梦都想着能把英语学习,可以使用一口流利的英文和洋鬼子交流,顺便忽悠下自己的同胞.没有地方学习英语,看还可以,网上有很多关于计算机的英文文献,写还行,说就完全不可能了.在以后的工作中慢慢 ...