HDU 4731 Minimum palindrome+找规律技巧

本文介绍了一种基于回文子串长度评估密码安全性的方法,并给出了一种构造最短回文子串的算法。该算法根据不同的字母数量生成特定长度的安全密码。

Minimum palindrome

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


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 <= 10 5)
 

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
2013 ACM/ICPC Asia Regional Chengdu Online

解题思路:
1,m=1的时候输出都是a
2,m=2的时候找规律咯,看代码比较容易理解,多推几个或许就知道了
3,m=3的时候,abc循环,不用知道为什么

这题以前接触过,老师拿来让我们再做一遍,只恨当初没补题,没有做出来,恨啊

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int main(){
    int T;
    scanf("%d",&T);
    int cas = 1 ;
    while(T--){
        scanf("%d%d",&m,&n);
        printf("Case #%d: ",cas++);
        if(m==1){
            for(int i=1;i<=n;i++)printf("a");
            printf("\n");
            continue ;
        }
        if(m==2){
            if(n==1)printf("a");
            else if(n==2)printf("ab");
            else if(n==3)printf("aab");
            else if(n==4)printf("aabb");
            else if(n==5)printf("aaaba");
            else if(n==6)printf("aaabab");
            else if(n==7)printf("aaababb");
            else if(n==8)printf("aaababbb");
            else if(n==9)printf("aaaababba");
            else{
                printf("aa");
                n-=2;
                for(int i=1;i<=n/6;i++){
                    printf("aababb");
                }
                if(n%6==1)printf("a");
                else if(n%6==2)printf("aa");
                else if(n%6==3)printf("aaa");
                else if(n%6==4)printf("aaaa");
                else if(n%6==5)printf("aabab");
            }
            printf("\n");
        }else{
            for(int i=1;i<=n/3;i++)printf("abc");
            if(n%3==1)printf("a");
            if(n%3==2)printf("ab");
            printf("\n");
        }

    }
    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值