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\n"); break;
                case 2:
                    printf("ab\n"); break;
                case 3:
                    printf("aab\n"); break;
                case 4:
                    printf("aabb\n"); break;
                case 5:
                    printf("aaaba\n"); break;
                case 6:
                    printf("aaabab\n"); break;
                case 7:
                    printf("aaababb\n"); break;
                case 8:
                    printf("aaababbb\n"); 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\n"); break;
                case 2:
                    printf("aa\n"); break;
                case 3:
                    printf("aaa\n"); break;
                case 4:
                    printf("aaaa\n"); break;
                case 5:
                    printf("aabab\n"); 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 )

HDOJ 4731 Minimum palindrome的更多相关文章

  1. HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. HDU 4731 Minimum palindrome 2013 ACM/ICPC 成都网络赛

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4731 题解:规律题,我们可以发现当m大于等于3时,abcabcabc……这个串的回文为1,并且字典数最小 ...

  3. HDU 4731 Minimum palindrome 打表找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...

  4. HDU 4731 Minimum palindrome (找规律)

    M=1:aaaaaaaa…… M=2:DFS+manacher, 暴出N=1~25的最优解,找规律.N<=8的时候直接输出,N>8时,头两个字母一定是aa,剩下的以aababb循环,最后剩 ...

  5. 2013 ACM/ICPC Asia Regional Chengdu Online 1004 Minimum palindrome

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. 逆序数2 HDOJ 1394 Minimum Inversion Number

    题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...

  7. 2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. HDOJ 3473 Minimum Sum

    划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  9. 【HDOJ】1513 Palindrome

    DP,MLE后改为滚动数组AC. #include <cstdio> #include <cstring> #include <cstdlib> #define M ...

随机推荐

  1. ios——MPMoviePlayerController截取视频缩略图 播放视频又可以截取视频缩略图

    #import <AVKit/AVKit.h>#import <MediaPlayer/MediaPlayer.h>#import "ViewController.h ...

  2. 20145314郑凯杰《信息安全系统设计基础》GDB调试32位汇编堆栈分析

    20145314郑凯杰<信息安全系统设计基础>GDB调试32位汇编堆栈分析 本篇博客将对第五周博客中的GDB调试32位汇编堆栈进行分析 首先放上以前环境配置的图: 图1: 测试代码: #i ...

  3. 20145215《Java程序设计》第9周学习总结

    20145215<Java程序设计>第九周学习总结 教材学习内容总结 整合数据库 JDBC入门 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操 ...

  4. try-catch和throw,throws的区别和联系

    转载:http://blog.sina.com.cn/s/blog_62148d1e0100hkqc.html 区别一:throw 是语句抛出一个异常:throws 是方法抛出一个异常: throw语 ...

  5. Bootstrap系列 -- 17. 复选框checkbox和单选择按钮radio

    Bootstrap框架中checkbox和radio有点特殊,Bootstrap针对他们做了一些特殊化处理,主要是checkbox和radio与label标签配合使用会出现一些小问题(最头痛的是对齐问 ...

  6. [vijos1892]树上的最大匹配(树形DP)

    题目:https://vijos.org/p/1892 分析:(100分其实用到各种c++优化,没什么实际意义,所以弄70就可以了) 题目很简单,很容易想出用树形DP,但是求方案数的时候,满满都是细节 ...

  7. [AHOI2013]找硬币(搜索)

    [Ahoi2013]找硬币 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 348  Solved: 114[Submit][Status] Descri ...

  8. Linux 容器的使用

    Linux 容器的使用 Linux 容器在 v2.6.29版本之后就加入到内核之中了, 之前虽然也听说过, 但一直没有太留心, 一直使用 KVM 来创建虚拟机. 直至最近 Docker 大出风头, 才 ...

  9. VS快速格式化代码

    Ctrl+E,D快速对所有文档进行格式化 Ctrl+E,F快速对选中内容进行格式化

  10. iOS--隐藏和显示TabBar的方法

    1.隐藏TabBar: - (void)hideTabBar { if (self.tabBarController.tabBar.hidden == YES) { return; } UIView  ...