【Codeforces 464A】No to Palindromes!
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
因为原序列没有任何长度超过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!的更多相关文章
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- [C和指针] 1-快速上手、2-基本概念、3-数据
第1章 快速上手 1.1.1 空白和注释 程序的空白的作用: 空行将程序的不同部分分割开来:制表符缩进语句,可以更好地显示程序的结构等等. 软件最大的开销并非在于编写,而是在于维护,所以需 ...
- HBuilder发行原装安装包操作记录
1.在左侧"项目管理器"中选择项目: 2.点击菜单栏中的"发行-->发行为原生安装包": 3.在弹出的App云端打包中填写相关信息: IOS开发者证书配置 ...
- Spring.Net学习笔记(二)-数据访问器
Spring对ADO.NET也提供了支持,依赖与程序集Spring.Data.dll IDbProvider IDbProvider定义了数据访问提供器的基础,配置如下 <?xml versio ...
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- Elasticsearch--建议器
目录 可用的建议器类型 term建议器 term建议器的配置选项 phrase建议器 completion建议器 在考虑性能的情况下,允许用户的拼写错误,以及构建一个自动完成功能 可用的建议器类型 t ...
- Android 你知道界面布局嵌套多少层之后会Crash吗
我们先放一张Hierarchy Viewer的图:(模拟器Android4.4) 看到数字6了吗,那个RelativeLayout是MainActivity的根ViewGroup, 而在Relativ ...
- POJ_2387_最短路
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46859 Accepted ...
- Clistctrl使用
CListCtrl控件使用方法总结 今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类 ...
- CAD制作简单动画
主要用到函数说明: IMxDrawEntity::Rotate 旋转一个对象.详细说明如下: 参数 说明 [in] IMxDrawPoint* basePoint 旋转基点 [in] DOUBLE d ...
- 04Servlet的生命周期
Servlet的生命周期 Servlet运行在Servlet容器中,其生命周期由容器来管理.Servlet的生命周期通过javax.servlet.Servlet接口中的init().service( ...