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

【题意】

题意

【题解】

因为原序列没有任何长度超过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. js工作备注

    { field : 'state', title : '事件状态', align: 'center', width : 120, formatter : function(value, row, in ...

  2. bzoj 2763: [JLOI2011]飞行路线【分层图+spfa】

    为什么早年的题总是从0开始标号啊--又zz了一次WA 分层图的题只有这一个套路吧,建分层图,然后优化时间是分层跑spfa然后层与层之间单独跑即可 #include<iostream> #i ...

  3. bzoj 2200: [Usaco2011 Jan]道路和航线【spfa】

    直接跑最短路就行了--还不用判负环 #include<iostream> #include<cstdio> #include<queue> using namesp ...

  4. (DP)51NOD 1118 机器人走方格

    M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果. Input 第1行,2个数M,N,中间用空格隔开.( ...

  5. [USACO09NOV]灯Lights

    题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...

  6. TFS修改了工作区

    计算机修改名字后,更换了TFS工作区,但原工作区的有些文件忘记签入: 解决方案: 删除原工作区即可,实现:到TFS工作区 - “管理工作区”,选中“显示远程工作区”,找到原工作区,删除即可.

  7. Linux shell命令之cat

    cat:查看文件的内容.连接文件.创建一个或多个文件和重定向输出到终端或文件  用法:cat [选项] [文件] 1. $ cat hello.txt 显示hello.txt文本文件中的内容 2. $ ...

  8. TCP/IP和UDP的比较

    TCP.UDP详解 1.传输层存在的必要性 由于网络层的分组传输是不可靠的,无法了解数据到达终点的时间,无法了解数据未达终点的状态.因此有必要增强网络层提供服务的服务质量. 2.引入传输层的原因 面向 ...

  9. Probabilistic locking in SQLite

    In SQLite, a reader/writer lock mechanism is required to control the multi-process concurrent access ...

  10. C/c++中 数组全局变量的定义声明 与 引用声明

    数组声明一次,所有的  定义声明  有切仅有一次! 别人遇到的问题如下: 在声明定义时,定义数组如下: int G_glob[100]; 在其他文件引用时声明如下: int *G_glob; 这样的操 ...