URAL 2045 Richness of words (回文子串,贪心)
Richness of words
题目链接:
http://acm.hust.edu.cn/vjudge/contest/126823#problem/J
Description
For each integer i from 1 to n, you must print a string s i of length n consisting of lowercase Latin letters. The string s i must contain exactly i distinct palindrome substrings. Two substrings are considered distinct if they are different as strings.
Input
The input contains one integer n (1 ≤ n ≤ 2000).
Output
You must print n lines. If for some i, the answer exists, print it in the form “ i : s i” where s i is one of possible strings. Otherwise, print “ i : NO”.
Sample Input
input output
4
1 : NO
2 : NO
3 : abca
4 : bbca
##题意:
要求输出n个长度为n的字符串(可以放26个小写字母):
要求Si中不同回文子串的个数恰为i.
##题解:
首先容易明确:n个相同字符组成的字符串的回文子串恰有n个.
那么对于小于n的情况,要想办法在上述基础上减少不同回文子串的个数.
减少的方式是使其出现重复的串. 而为了使得重复的串之间不构成新得回文串,必须插入不相同的字符.
可以证明,当插入连续的"bc"后,无论怎样都不可能构成新的回文串.
对于Si: 先输出i-2个'a',再输出"bc", 此时的i个字符恰好构成i个回文字串,为了使后面的字符不构成新回文串,可以不断填充"abc".
注意,此题时限比较严格,容易TLE.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 210000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d", &n) != EOF)
{
if(n == 1) {
printf("1 : a\n");
continue;
}
if(n == 2) {
printf("1 : NO\n");
printf("2 : ab\n");
continue;
}
printf("1 : NO\n");
printf("2 : NO\n");
for(int i=3; i<=n; i++) {
printf("%d : ", i);
for(int j=1; j<=i-2; j++) putchar('a');
printf("bc");
for(int j=i+1,k=0; j<=n; j++,k=(k+1)%3) {
if(k==0) putchar('a');
if(k==1) putchar('b');
if(k==2) putchar('c');
}
printf("\n");
}
}
return 0;
}
URAL 2045 Richness of words (回文子串,贪心)的更多相关文章
- URAL 1297 Palindrome 最长回文子串
POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...
- URAL 2037 Richness of binary words (回文子串,找规律)
Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...
- URAL 1297 最长回文子串(后缀数组)
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297
1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a ...
- Ural 1297 Palindrome 【最长回文子串】
最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...
- URAL 1297 后缀数组:求最长回文子串
思路:这题下午搞了然后一直WA,后面就看了Discuss,里面有个数组:ABCDEFDCBA,这个我输出ABCD,所以错了. 然后才知道自己写的后缀数组对这个回文子串有bug,然后就不知道怎么改了. ...
- Ural 1297 Palindrome(后缀数组+最长回文子串)
https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...
- ural 1297 后缀数组 最长回文子串
https://vjudge.net/problem/URAL-1297 题意: 给出一个字符串求最长回文子串 代码: //论文题,把字符串反过来复制一遍到后边,中间用一个没出现的字符隔开,然后就是枚 ...
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
随机推荐
- ubuntu下文件压缩/解压缩命令总结
.gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 解压:tar zxvf FileName.tar ...
- Linux命令之chmod 及+s 参数(临时以所有者权限执行)
转自: http://blog.csdn.net/shaobingj126/article/details/7031221 chmod用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该 ...
- C#实现GDI+基本图的缩放、拖拽、移动
C#实现GDI+基本图的缩放.拖拽.移动示例代码如下: using System;using System.Collections.Generic;using System.ComponentMode ...
- Miller-Rabin素数测试学习小计
1.Miller-Rabin是干啥的?它是用来检测一个数字(一般是很大的数字)是不是素数: 2.Miller-Rabin算法基于的两个定理: (1)费尔马小定理:如果p是一个素数,且0<a< ...
- 详谈 Jquery Ajax 异步处理Json数据.
啥叫异步,啥叫Ajax.咱不谈啥XMLHTTPRequest.通俗讲异步就是前台页面javascript能调用后台方法.这样就达到了无刷新.所谓的Ajax.这里我们讲二种方法 方法一:(微软有自带Aj ...
- easyui-dialog中文件上传处理
function openDialog() { // $('#dlg').dialog('open'); //EasyUi的dialog中文件上传,后台获取不到文件,需要改写为下面这样 $(" ...
- BZOJ2151: 种树
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2151 题解:此题=数据备份.喜闻乐见挂链表. 代码: #include<cstdio&g ...
- [转]Jquery Ajax用法
原文地址:http://www.php100.com/html/program/jquery/2013/0905/6004.html jQuery学习之jQuery Ajax用法详解 来源: 时间 ...
- 一天一个Java基础——泛型
这学期的新课——设计模式,由我仰慕已久的老师传授,可惜思维过快,第一节就被老师挑中上去敲代码,自此在心里烙下了阴影,都是Java基础欠下的债 这学期的新课——算法设计与分析,虽老师不爱与同学互动式的讲 ...
- Oracle 一次 锁表 处理小记
同事说测试库上的一张表被锁了. 不能执行DML 操作. 锁表的准确说法应该是阻塞.之前的一遍blog里有说明: 锁 死锁 阻塞Latch 等待 详解 http://blog.csdn.net/tian ...