您的位置:首页 > 其它

URAL 2037 Richness of binary words

2015-04-23 19:45 197 查看
B - Richness of binary words
Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice URAL
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

inputoutput
4

1 : NO
2 : NO
3 : NO
4 : aaaa

一个字符串只能由字符a b组成 给定一个数字n 问从1~n 能不能有一个字符串 使得这个字符串里面的回文子串的个数有i个 有就输出这个字符串 否则就输出NO

一开始模拟了很多样例。。。对于长度为n的字符串只能找到回文子串个数为n的 所以就猜想除了n 其他都是NO

然后就WA了

后来 暴力了之后发现一个字符串出现的比较频繁 abbaba

后来发现这是一个循环结 整个的字符串的个数是跟这段字符串有关的。。。

我是搬运工

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

inline int Read()
{
char ch;
int a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}

inline void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}

char ss[] = "abbaba";

int main()
{
//fread;
int n;
while(~scanf("%d", &n))
{
if(n <= 7)
{
for(int i = 1; i < n; i++)
{
Print(i);
puts(" : NO");
}
Print(n);
putchar(' '), putchar(':'), putchar(' ');
for(int i = 1; i <= n; i++) putchar('a');
puts("");
}
else if(n == 8)
{
for(int i = 1; i < 7; i++)
{
Print(i);
puts(" : NO");
}
puts("7 : aabbabaa");
Print(n);
putchar(' '), putchar(':'), putchar(' ');
for(int i = 1; i <= n; i++) putchar('a');
puts("");
}
else
{
for(int i = 1; i <= 7; i++)
{
Print(i);
puts(" : NO");
}
for(int i = 8; i < n; i++)
{

Print(i);
putchar(' '), putchar(':'), putchar(' ');
for(int j = 0; j < i - 8 + 1; j++) putchar('a');
int loop = (n - (i - 8 + 1)) / 6, res = (n - (i - 8 + 1)) % 6;
while(loop--)
{
// printf("abbaba");
putchar('a');
putchar('b');
putchar('b');
putchar('a');
putchar('b');
putchar('a');
}
for(int j = 0; j < res; j++) putchar(ss[j]);
puts("");
}
Print(n);
putchar(' '), putchar(':'), putchar(' ');
// printf("%d : ", n);
for(int i = 1; i <= n; i++) putchar('a');
puts("");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: