2013成都網路賽1004題HDU 4731Minimum palindrome (思維題目)
Minimum palindrome
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 254 Accepted Submission(s): 123
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.
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)
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
題目大意:給你一個m,n.用a,b,c,d,e一共m種字母組成長度為n的字串中的最長迴文子串長度最短。如果有多組長度一樣,輸出字典序最小的。開始吉吉以為是搜尋,然後就開始寫搜尋,然後用了manacher匹配演算法。記得aaaababbaababbaa這樣的在開始組隊賽的時候遇到過類似的。
解題思路:如果m為1,直接輸出。如果m>=3直接輸出abcabc這樣的可以使得最長迴文長度為1.現在主要是討論m為2的情況,然後就開始列舉了,開始找規律。a,ab,aab,aabb,aaaba,aaabab,aaababb,aaababbb。這是1~8的情況,發現當n=9的時候迴文長度至少得為4.然後就先輸出aaaa,然後找使得迴文長度最長為4且字典序儘可能小的。發現是babbaababbaa.....如此便得到迴圈串babbaa.後面就好處理了。具體實現見程式碼。
題目地址:Minimum palindrome
AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
char p[8][10]={"a","ab","aab","aabb","aaaba","aaabab","aaababb","aaababbb"};
char a[6]={'b','a','b','b','a','a'};
int main()
{
int tes,i;
int cas=0;
scanf("%d",&tes);
while(tes--)
{
int m,n;
printf("Case #%d: ",++cas);
scanf("%d%d",&m,&n);
if(n==1)
cout<<"a"<<endl;
else if(m==1)
{
for(i=0;i<n;i++)
cout<<"a";
cout<<endl;
}
else if(m==2)
{
if(n<=8) //迴文長度最多為3
cout<<p[n-1]<<endl;
else //迴文長度為4
{
cout<<"aaaa";
n-=4;
int tmp=n/6;
for(i=0;i<tmp;i++)
cout<<"babbaa";
tmp=n-tmp*6;
for(i=0;i<tmp;i++)
cout<<a[i];
cout<<endl;
}
}
else
{
int tmp=n/3;
for(i=0;i<tmp;i++)
cout<<"abc";
if(n-tmp*3==2)
cout<<"ab";
else if(n-tmp*3==1)
cout<<"a";
cout<<endl;
}
}
return 0;
}
//15MS 272K
相關文章
- 2013成都網路賽 兩個簡單題
- 2013杭州網路賽C題HDU 4640(模擬)
- 2014北京網路賽1006||hdu5037 思維題
- 2013長沙網路賽H題Hypersphere (蛋疼的題目 神似邀請賽A題)
- 2013成都站F題||hdu4786 並查集 生成樹並查集
- hdu4400 STL應用 查詢思維題
- 2014廣州網路賽1004||hdu5025 分層最短路
- 2013長沙網路賽 E題(水題 有點小bug)
- hdu5452 || 瀋陽網路賽1003題 最近公共祖先問題
- 2014鞍山網路賽 E題||hdu 5001 概率dp
- 2014上海網路賽1004||hdu5045 contest【狀態壓縮dp】
- 挑選方案問題(牛客競賽 思維題+推導公式)公式
- HDU4722Good Numbers熱身賽2 1007題(思維題 不用數位dp照樣可以做的)Go
- 2013杭州網路賽D題HDU 4741(計算幾何 解三元一次方程組)
- hdu5445 || 2015長春網路賽1009題 多重揹包問題
- HDU 4496D-City2013通化邀請賽D題(並查集 需要壓縮路徑)並查集
- 演算法設計幾個經典思維題目演算法
- 2013成都站D題||hdu4784 bfs+DP+priority_queue(思路從spfa得到啟示)
- 威馬汽車閆楓:造車新賽道的網際網路思維
- 面試中網路相關題目面試
- 啥叫網際網路思維
- 網際網路公司,網際網路時代的特徵(移動網際網路,平臺思維,網際網路思維)特徵
- 網路安全實驗室題目(選擇題篇)
- 組合計數思維題
- 2014上海網路賽1004||hdu5045 二分圖的最佳匹配 或 狀態壓縮dp
- 28張圖清楚“網際網路思維”
- HDU 4119Isabella's Message2011成都現場賽I題(字串模擬)字串
- 網際網路思維 傲慢的鬧劇
- ABAP 常見面試題目【網路資源】面試題
- 2018 徐州網路賽 G 題解
- 網際網路公司面試必問的Redis題目面試Redis
- 第三屆華中地區邀請賽網路賽題解
- 怎麼凸顯主題?極思維
- 測試筆試邏輯思維題筆試
- iOS-邏輯思維面試題01iOS面試題
- 搞定字串類面試題-Palindrome字串面試題
- 周朝陽:我眼中的網際網路思維
- 當圖靈社群遇上“網際網路思維”圖靈