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

【题意】

题意

【题解】

因为原序列没有任何长度超过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. c++编程中处理char和wchar_t的好工具

    /* ttype.h sdragonx 2015-02-18 18:32:43 这个几个模版函数是为了处理ansi或unicode,使字符串值或者字符串函数能够在模版中使用 2018/7/26 23: ...

  2. QQ自动登录Demo源码(附全套WindowsApi)

    在开发过程中,偶尔会有自动化操作软件的需求,便想到用句柄实现自动化的功能,记录下知识点,以作备忘. 实现流程: 获取窗口句柄,根据定位获取input,调用windowsapi模拟鼠标点击, 输入 , ...

  3. 递推DP HDOJ 5389 Zero Escape

    题目传送门 /* 题意:把N个数分成两组,一组加起来是A,一组加起来是B,1<=A,B<=9,也可以全分到同一组.其中加是按照他给的规则加,就是一位一位加,超过一位数了再拆分成一位一位加. ...

  4. ACM_错排(递推dp)

    RPG的错排 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今年暑假GOJ集训队第一次组成女生队,其中有一队叫RPG,但做为集训 ...

  5. spring的依赖注入如何降低了耦合

    依赖注入:程序运行过程中,如需另一个对象协作(调用它的方法.访问他的属性时),无须在代码中创建被调用者,而是依赖于外部容器的注入 看过一些比较好的回答 1.一个人(Java实例,调用者)需要一把斧子( ...

  6. php pdo oracle

    <?php/** * Created by mestars. * User: mestars * Date: 6/13/16 * Time: 10:52 PM */header('Access- ...

  7. 树莓派 关闭屏保 / RaspberryPi turn off ScreenSaver / RaspberryPi disable screen off

    安装xscreensaver并配置 见:https://www.raspberrypi.org/forums/viewtopic.php?t=57552

  8. Java 8 (10) CompletableFuture:组合式异步编程

    随着多核处理器的出现,提升应用程序的处理速度最有效的方式就是可以编写出发挥多核能力的软件,我们已经可以通过切分大型的任务,让每个子任务并行运行,使用线程的方式,分支/合并框架(java 7) 和并行流 ...

  9. Spring Boot (31) 数据验证

    曾经参数的验证是这样的: public String test(User user){ if(user == null){ throw new NullPointerException("u ...

  10. LN : Eden Bitset_3

    Appreciation to our TA, 王毅峰, who designed this task. 问题描述 Give you N numbers a[1]...a[n] and M numbe ...