HDU 4731 Minimum palindrome+找规律技巧

    xiaoxiao2025-01-25  3

    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; }
    转载请注明原文地址: https://ju.6miu.com/read-1295760.html
    最新回复(0)