Codeforces Round #411 B. 3-palindrome

B. 3-palindrome
time limit per test
1 second
memory limit per test
256 megabytes
 

In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.

He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.

Output

Print the string that satisfies all the constraints.

If there are multiple answers, print any of them.

 
input
2
output
aa
input
3
output
bba
Note:

palindrome is a sequence of characters which reads the same backward and forward.

 

---------------------------------------华丽的分割线--------------------------------------------

 

题目大意: 输入一个整数n,代表字符串的长度

     输出一个字符串(该字符串自包含a、b、c三种字符,且不会出现长度为3的回文字符串,保证c字符的个数出现的尽量少)

 

解题思路:

     该题最重要的要保证字符串中不会出现长度为3的回文字符串,我们可以先设计一个“子串”,保证以这个子串为基础的字符串不会出现长度为3的回文字符串即可

     例如 “aabb” 当然也可以是别的哈(但是长度要尽量的短)

     题目要求c字符的个数要尽可能的少,我们不输出c字符就好了。

AC代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <stdlib.h>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int main ()
 9 {
10     int n,i;
11     while (~scanf("%d",&n))
12     {
13         if (n == 1)         
14             printf("a\n");
15         else if (n == 2)
16             printf("ab\n");
17         else if(n == 3)
18             printf("aab\n");
19         else{
20             for (i = 0; i < n/4; i ++)   // 处理最后剩余长度不足4的情况
21                 printf("aabb");
22             for (i = 0; i < n%4; i ++){
23                 if (i >= 2)
24                     printf("b");
25                 else
26                     printf("a");
27             }
28             printf("\n");
29         }
30     }
31     return 0;
32 }

 

posted @ 2017-05-16 13:52  gaoyanliang  阅读(340)  评论(0编辑  收藏  举报