J - Richness of words

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 idistinct 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<=2000),for(int i=1;i<=n;i++),如果存在一个长为n的字符串,且其中有i个回文串,则输出

该字符串,否则输出NO;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e6+10; char s[2005][2005];
int flag[2005];
int main()
{
int n;
while(~scanf("%d",&n))
{
MM(s,'\0');
if(n==1) {printf("1 : a\n");continue;}
if(n==2) {
printf("1 : NO\n");
printf("2 : ab\n");
continue;
} for(int i=1;i<=3;i++) s[n][i]='a'+i-1;
for(int i=4;i<=n;i++) s[n][i]='z';
int p=3;
for(int i=n-1;i>=1;i--)
{
int j;
for(j=1;j<=p;j++)
s[i][j]=s[i+1][j];
if(s[i][p]=='a') s[i][j]='b';
if(s[i][p]=='b') s[i][j]='c';
if(s[i][p]=='c') s[i][j]='a';
for(j++;j<=n;j++) s[i][j]='z';
p++;
}
for(int i=1;i<=2;i++) printf("%d : NO\n",i);
for(int i=3;i<=n;i++) printf("%d : %s\n",i,s[i]+1);
}
return 0;
}

  分析:刚开始想的是比如n==30,,那么首先就是

aaaaabcdefg...xyz,

aaaaabcaefg....zyz

aaaaabcaafg....xyz;

这样下去,,但是其实这样不是最优的;

正确做法:考虑abc三个字符,无论怎样循环都是不会有回文出现的,比如abcabcabc....

那么对于n==30的情况;

abczzzzzz...z;//30

abcazzzzz...z;//29

abcabzzzz...z;//28

abcabczzz...z;//27

abcabcazz...z;//26

所以这样下去,每次都能保证这一个比上面一个减1,然后特判下2,3;

最新文章

  1. centos7 apache httpd安装和配置django项目
  2. CozyRSS开发记录18-番外之Atom1.0的支持
  3. Glide.js:响应式 &amp; 触摸友好的 jQuery 滑块插件
  4. Codeforces 721D [贪心]
  5. Struts2笔记——result结果类型
  6. vmware克隆centos6.5 导致 system eth0 不可用解决办法
  7. CCNA实验(10) -- Access List
  8. 【3】JavaScript编程全解笔记(三)
  9. U盘做svn版本控制
  10. C#中:函数访问级别对函数形参访问级别的约束
  11. 在Vim按了ctrl+s后
  12. JavaWeb工程中web.xml基本配置(转载学习)
  13. Floyd算法——计算图中任意两点之间的最短路径
  14. Netsharp系列文章目录结构
  15. C#深度学习の枚举类型(IEnumerator,IEnumerable)
  16. Codeforces Round #533 (Div. 2) A. Salem and Sticks(暴力)
  17. There is no setter for property named 可能产生的原因!
  18. JS 模拟 重载
  19. [阮一峰]Linux 守护进程的启动方法
  20. 如何解决Mac只能登QQ不能联网

热门文章

  1. django初步了解3
  2. Web项目测试流程总结
  3. golang 客户端
  4. 牛客 40F 珂朵莉的约数 (莫队)
  5. python爬虫下正则各种字符串数据匹配
  6. latex公式居中环境
  7. 基于Dockerfile搭建JAVA Tomcat运行环境
  8. Linux命令详解——vmstat
  9. Java面向对象(一)
  10. Linux useradd userdel groupadd groupdel gpasswd(组成员管理) id groups