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. [30分钟]MSSQL快速入门教程

    1.什么是SQL语句 sql语言:结构化的查询语言.(Structured Query Language),是关系数据库管理系统的标准语言. 它是一种解释语言:写一句执行一句,不需要整体编译执行.语法 ...

  2. jquery的基本动画方法

    1 在使用$.extent()的时候,我们一般不放function类型,如果放的话,提前测试下. ?2 Function类型是一种基本类型还是引用类型呢. 3 $('<div>') 指创建 ...

  3. SequoiaDB 系列之一 :SequoiaDB的安装、部署

    在分析或者参与一个开源项目之前,了解项目构建的目的是有必要的. 既然SequoiaDB是NoSQL数据库产品,则必然存在于传统关系型数据库相同的功能点:数据的增.删.改和查询(CRUD). 先了解怎么 ...

  4. 编写高质量代码改善C#程序的157个建议[勿选List<T>做基类、迭代器是只读的、慎用集合可写属性]

    前言 本文已更新至http://www.cnblogs.com/aehyok/p/3624579.html .本文主要学习记录以下内容: 建议23.避免将List<T>作为自定义集合类的基 ...

  5. 阿里百川IMSDK--自定义群聊界面

    // 获取群对象 YWTribe *tribe = [self.tribesArray objectAtIndex:indexPath.row]; // 发起群聊 UIViewController * ...

  6. 第一次作业---安卓开发工具Android studio发展演变

    Android studio2013年由谷歌推出,用于安卓端的开发,我所使用的版本为2015年5月推出的1.3.2. 1.安装.配置.作为麻瓜的我,刚刚接触Android studio时在安装方面走了 ...

  7. java操作xml

    package com.xml.zh; import javax.xml.parsers.*; import org.w3c.dom.*; public class XmlTest1{ /** * 使 ...

  8. G-nav-01

    <body><header id="masthead" class="masthead" role="banner"> ...

  9. C/C++语言算法题——替换

    [问题] Description 给定一个有限长度的非负整数序列.一次操作是指从第一个元素开始,依次把数列中的每个数替换为它右边比它小的数的个数.对该数列不断进行这个操作.总有一个时刻该数列将不再发生 ...

  10. ie6下使用js替换img标签src属性图片不显示的错误

    首先,我必须再次强调一下,F-U-C-K I-E! 其次,简单阐述一下这个bug的出现的情况.页面中有个<a href=”javascript:void(0)” onclick=”swapImg ...