zoukankan      html  css  js  c++  java
  • 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
    ");
    15         else if (n == 2)
    16             printf("ab
    ");
    17         else if(n == 3)
    18             printf("aab
    ");
    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("
    ");
    29         }
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    字符串Hash 学习笔记
    P4315 月下“毛景树” 题解
    page
    Equation
    Graph
    配置UOJ数据的正确姿势
    luogu2261余数求和题解--整除分块
    luogu2858奶牛零食题解--区间DP
    luogu1005矩阵取数游戏题解--区间DP
    luogu4677山区建小学题解--区间DP
  • 原文地址:https://www.cnblogs.com/yoke/p/6861014.html
Copyright © 2011-2022 走看看