zoukankan      html  css  js  c++  java
  • HDOJ 4731 Minimum palindrome



    Minimum palindrome

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 54    Accepted Submission(s): 18


    Problem Description
    Setting password is very important, especially when you have so many "interesting" things in "F:TDDOWNLOAD".
    We define the safety of a password by a value. First, we find all the substrings of the password. Then we calculate the maximum length of those substrings which, at the meantime, is a palindrome.
    A palindrome is a string that will be the same when writing backwards. For example, aba, abba,abcba are all palindromes, but abcab, abab are not.
    A substring of S is a continous string cut from S. bcd, cd are the substrings of abcde, but acd,ce are not. Note that abcde is also the substring of abcde.
    The smaller the value is, the safer the password will be.
    You want to set your password using the first M letters from the alphabet, and its length should be N. Output a password with the smallest value. If there are multiple solutions, output the lexicographically smallest one.
    All the letters are lowercase.
     

    Input
    The first line has a number T (T <= 15) , indicating the number of test cases.
    For each test case, there is a single line with two integers M and N, as described above.(1 <= M <= 26, 1 <= N <= 105)
     

    Output
    For test case X, output "Case #X: " first, then output the best password.
     

    Sample Input
    2
    2 2
    2 3
     

    Sample Output
    Case #1: ab
    Case #2: aab
     

    Source
     

    Recommend
    liuyiding
     
     

    3个及3个以上字母就可以不构成回文串,2个字母的打表找规律

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    int main()
    {
        int T;
        int cas=1;
        scanf("%d",&T);
        while(T--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            printf("Case #%d: ",cas++);
            if(n==1)
            {
                while(m--)
                    putchar('a');
                putchar(10);
                continue;
            }
            if(n==2)
            {
                if(m<=8)
                {
                    switch(m)
                    {
                    case 1:
                        printf("a "); break;
                    case 2:
                        printf("ab "); break;
                    case 3:
                        printf("aab "); break;
                    case 4:
                        printf("aabb "); break;
                    case 5:
                        printf("aaaba "); break;
                    case 6:
                        printf("aaabab "); break;
                    case 7:
                        printf("aaababb "); break;
                    case 8:
                        printf("aaababbb "); break;
                    }
                    continue ;
                }
                else
                {
                    printf("aaaababb");
                    m-=8;
                    int l=m/6;
                    for(int i=0;i<l;i++)
                    {
                        printf("aababb");
                    }
                    l=m%6;
                    switch(l)
                    {
                    case 1:
                        printf("a "); break;
                    case 2:
                        printf("aa "); break;
                    case 3:
                        printf("aaa "); break;
                    case 4:
                        printf("aaaa "); break;
                    case 5:
                        printf("aabab "); break;
                    }
                }
            }
            else
            {
                for(int i=0;i<m;i++)
                {
                    putchar('a'+(i)%3);
                }
                putchar(10);
            }
        }

        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Codeblocks )

  • 相关阅读:
    Mysql常用sql语句(6)- limit 限制查询结果的条数
    Mysql常用sql语句(5)- as 设置别名
    Mysql常用sql语句(4)- distinct 去重数据
    Mysql常用sql语句(3)- select 查询语句基础使用
    Jenkins(8)- CentOS 7.x 通过yum安装jenkins
    Jmeter系列(11)- 并发线程组Concurrency Thread Group详解
    Jmeter系列(10)- 阶梯加压线程组Stepping Thread Group详解
    Jmeter系列(9)- jmeter插件入门篇
    Jmeter系列(8)- test plam测试计划参数详解
    Jmeter系列(6)- test plan测试计划详细讲解
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350853.html
Copyright © 2011-2022 走看看