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
一个字符串只能由字符a b组成 给定一个数字n 问从1~n 能不能有一个字符串 使得这个字符串里面的回文子串的个数有i个 有就输出这个字符串 否则就输出NO
一开始模拟了很多样例。。。对于长度为n的字符串只能找到回文子串个数为n的 所以就猜想除了n 其他都是NO
然后就WA了
后来 暴力了之后发现一个字符串出现的比较频繁 abbaba
后来发现这是一个循环结 整个的字符串的个数是跟这段字符串有关的。。。
我是搬运工
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
input | output |
---|---|
4 | 1 : NO 2 : NO 3 : NO 4 : aaaa |
一开始模拟了很多样例。。。对于长度为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;
}
相关文章推荐
- Ural 2037 Richness of binary words
- Ural 2045 Richness of words(构造)
- Ural 2045 Richness of words
- Timus Online Judge 2045 Richness of words
- LeetCode *** 236. Lowest Common Ancestor of a Binary Tree
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- URAL - 1057 Amount of Degrees
- 236. Lowest Common Ancestor of a Binary Tree
- My Solution to Lowest Common Ancestor of a Binary Tree Part I(Bottom-up Approach)
- LeetCode 之 Maximum Depth of Binary Tree
- leetcode 30. Substring with Concatenation of All Words
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
- Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- leetcode-30 Substring with Concatenation of All Words
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- 30. Substring with Concatenation of All Words
- [leetcode]Minimum Depth of Binary Tree
- Substring with Concatenation of All Words
- Kaggle情感分析(Bag of Words Meets Bags of Popcorn)