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 ...
随机推荐
- excel文档
1.快速统计行数(ctrl+Shift+(方向键向下)). bson数据类型 留个影响 public enum BsonType { Double = 0x01, String = 0x02, Doc ...
- C#编写媒体播放器--Microsoft的Directx提供的DirectShow组件,该组件的程序集QuartzTypeLib.dll.
使用C#编写媒体播放器时,需要用到Microsoft的Directx提供的DirectShow组件.用该组件前需要先注册程序集QuartzTypeLib.dll. 1.用QuartzTypeLib.d ...
- JEE学习线路
传智播客:javaEE学习线路以及需要掌握的知识点:http://java.itcast.cn/subject/javastudypath/index.shtml 最近在学JavaEE,没学Java ...
- Codeforces Beta Round #2B(dp+数学)
贡献了一列WA.. 数学很神奇啊 这个题的关键是怎么才能算尾0的个数 只能相乘 可以想一下所有一位数相乘 除0之外,只有2和5相乘才能得到0 当然那些本身带0的多位数 里面肯定含有多少尾0 就含有多少 ...
- fil_space_t
typedef struct fil_space_struct fil_space_t; /** Tablespace or log data space: let us call them by a ...
- bzoj1562
很明显是二分图匹配,关键是怎么求字典序最小 想到两种做法,首先是直接匹配,然后从第一位贪心调整 第二种是从最后一个倒着匹配,每次匹配都尽量选小的,这样一定能保证字典序最小 type node=reco ...
- How to delete a team project from Team Foundation Service (tfs.visualstudio.com)
C:\project>tfsdeleteproject /collection:https://buckh-test2.visualstudio.com/DefaultCollection Te ...
- Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
- 基于Live555,ffmpeg的RTSP播放器直播与点播
基于Live555,ffmpeg的RTSP播放器直播与点播 多路RTSP高清视频播放器下载地址:http://download.csdn.net/detail/u011352914/6604437多路 ...
- Android UI——分享一个登录缓冲界面
效果如上图 所示 :就是下面的 loading 字母会按顺序一个个的 动起来 ,很好的效果 代码说明 请参考 该文:http://blog.csdn.net/xyz_lmn/article/deta ...