Ural 2037. Richness of binary words 打表找规律 构造
2037. Richness of binary words
题目连接:
http://acm.timus.ru/problem.aspx?space=1&num=2037
Description
For each integer i from 1 to n, you must print a string si of length n consisting of letters ‘a’ and ‘b’ only. The string si 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 : si” where si is one of possible strings. Otherwise, print “i : NO”.
Sample Input
4
Sample Output
1 : NO
2 : NO
3 : NO
4 : aaaa
Hint
题意
让你构造长度为n,字符集为2,且本质不同的回文串恰好i个的字符
无解输出no
题解:
打表找规律,比较容易发现答案其实就是aababb->aaababb->aaaababb的循环,这样的。
然后输出就好了。
代码
#include <bits/stdc++.h>
using namespace std;
int N;
void solve_special(){
if( N <= 7 ){
for(int i = 1 ; i < N ; ++ i) printf("%d : NO\n" , i);
printf("%d : ",N);
for(int j = 1 ; j <= N ; ++ j) putchar('a');
puts("");
}else if( N == 8 ){
cout << "1 : NO" << endl;
cout << "2 : NO" << endl;
cout << "3 : NO" << endl;
cout << "4 : NO" << endl;
cout << "5 : NO" << endl;
cout << "6 : NO" << endl;
cout << "7 : aababbaa" << endl;
cout << "8 : aaaaaaaa" << endl;
}else if( N == 9 ){
cout << "1 : NO" << endl;
cout << "2 : NO" << endl;
cout << "3 : NO" << endl;
cout << "4 : NO" << endl;
cout << "5 : NO" << endl;
cout << "6 : NO" << endl;
cout << "7 : NO" << endl;
cout << "8 : aaababbaa" << endl;
cout << "9 : aaaaaaaaa" << endl;
}else if( N == 10 ){
cout << "1 : NO" << endl;
cout << "2 : NO" << endl;
cout << "3 : NO" << endl;
cout << "4 : NO" << endl;
cout << "5 : NO" << endl;
cout << "6 : NO" << endl;
cout << "7 : NO" << endl;
cout << "8 : aaababbaaa" << endl;
cout << "9 : aaaababbaa" << endl;
cout << "10 : aaaaaaaaaa" << endl;
}
}
void solve(){
int target = (N-11)/2 + 2 ;
for(int i = 1 ; i < 8 ; ++ i){
printf("%d : NO\n" , i);
}
int cur = 2;
for(int i = 8 ; i < N ; ++ i){
int len = 0 , flag = 0 , rs = cur;
printf("%d : " , i);
while( len < N ){
if( flag == 0 ){
putchar('a');
-- rs;
}else if( flag == 1 ){
if( rs == 3 ) putchar('a');
else putchar('b');
-- rs;
}
if( rs == 0 ){
flag ^= 1;
if( flag == 0 ) rs = cur;
else rs = 4;
}
++ len;
}
if( cur == target ) cur += 2;
else ++ cur;
puts("");
}
printf("%d : ",N);
for(int i = 1 ; i <= N ; ++ i) putchar('a');
puts("");
}
int main(int argc,char *argv[]){
//freopen("KO.txt","w",stdout);
scanf("%d",&N);
if( N < 10 ) solve_special();
else if( N == 10 ){
cout << "1 : NO" << endl;
cout << "2 : NO" << endl;
cout << "3 : NO" << endl;
cout << "4 : NO" << endl;
cout << "5 : NO" << endl;
cout << "6 : NO" << endl;
cout << "7 : NO" << endl;
cout << "8 : aaababbaaa" << endl;
cout << "9 : aaaababbaa" << endl;
cout << "10 : aaaaaaaaaa" << endl;
}else solve();
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!
2014-08-05 [转]很特别的一个动态规划入门教程
2014-08-05 wikioi 1098 均分纸牌