Problem Description:
S is a string of length n. S consists of lowercase English alphabets.
Your task is to count the number of different S with the minimum number of distinct sub-palindromes. Sub-palindrome is a palindromic substring.
Two sub-palindromes u and v are distinct if their lengths are different or for some i (0≤i≤length),
u
i
u_i
ui≠
v
i
v_i
vi. For example, string “aaaa” contains only 4 distinct sub-palindromes which are “a”, “aa”, “aaa” and “aaaa”.ui≠vi. For example, string “aaaa” contains only 4 distinct sub-palindromes which are “a”, “aa”, “aaa” and “aaaa”.
Key:找出数量最少的子回文串(长度可以不同),求他们构成不同字符串S的数量。
推导:
n=1时,子回文种类数最少为1,S的数量为26。
n=2时,S的类型有aa,ab(ba)两种,第一种子串有a,aa;第二种子串有a,b,即最少子回文种类数为2。S的数量为2626=676。
n=3时,S的类型有aaa,aab,aba,baa,abc等,aaa子回文有a,aa,aaa;aab子回文有a,aa,b;aba子回文有a,b,aba;baa子回文有b,a,aa;abc子回文有a,b,c,经推导最少子回文种类数为3.S的数量为2626*26=17576.
n=4时,S的类型有aaaa,aaab,aabb,abca,abcd等,aaaa,aaab,aabb,abcd等子回文种类数为4,abca子回文a,b,c为3,即最少子回文种类数为3,S的数量就是从26字母选三个A(26,3)=15600。
以此类推,可得n=5,6,…时,最少子回文种类数均为3,且S类型为abcabcabc…;所以S的数量均为15600
代码如下
#include<iostream>
int main(){
int t;
scanf("%d",&t);
int n[t];
for(int i = 0;i < t;i++){
scanf("%d",&n[i]);
}
for(int i = 0;i < t;i++){
if(n[i]==1){printf("26\n");}
else if(n[i]==2){printf("676\n");}
else if(n[i]==3){printf("17576\n");}
else{printf("15600\n");}
}
return 0;
}