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. 关于json 与 Request Header 的Content-Type 一些关系。

    由于最近遇到关于,ashx文件ajax解析参数的问题.查询网上很多资料后,已经解决. 鉴于网上已经足够多的,关于这个问题的文章.大部分内容来自互联网,我这里只是做一些整理和记录.特此说明并非原创. C ...

  2. JavaScript实例---表格隔行变色以及移入鼠标高亮

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  3. 微信支付开发-Senparc.Weixin.MP详解

    年底了,反而工作更忙了,我从15年11月开始写<1024伐木累>系列小说和爆笑对白,得到了很多身边的技术好友的支持,现在爆笑对白已经有越来越多的朋友一起帮着写段子,整理,包括小说内容的编辑 ...

  4. C#6.0特性(快来围观)

    说明一下,很多博友一进来就认为仅仅是语法糖,C#语法的更新,代表着它的进步,语法糖是为了让我们更好的实现语句和功能,增加了易读性和易用性.而且它的每次进步,也会给我们带来新的支持和改进.比如C#(4. ...

  5. [bzoj1296][SCOI2009]粉刷匠(泛化背包)

    http://www.lydsy.com:808/JudgeOnline/problem.php?id=1296 分析: 首先预处理出每一行的g[0..T]表示这一行刷0..T次,最多得到的正确格子数 ...

  6. linux oracle磁盘满了

    最近,查看我们一台linux服务器,发现硬盘空间都已经使用了95%,很是疑惑啊,怎么回事那?难道是数据库文件太大了? Filesystem            Size  Used Avail Us ...

  7. log4j:WARN Please initialize the log4j system properly.解决

    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA ...

  8. hdu1873优先队列

    #include<stdio.h> #include<queue> using namespace std; struct node { int id; int val; fr ...

  9. awk多文件操作

    awk 多文件操作2种实现方法 我们经常会将2个有关联文本文件进行合并处理.分别从不同文件获取需要的列,然后, 整体输出到一起.awk进行多文件处理时候,常常会遇到2个方面问题,第一个是怎么样合并多个 ...

  10. Hive简单优化;workflow调试

    1. 定义job名字 SET mapred.job.name='customer_rfm_analysis_L1'; 这样在job任务列表里可以第一眼找到自己的任务. 2. 少用distinct, 尽 ...