Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He has 26 photos, labeled "a" to "z", and he has compiled them into a photo booklet with some photos in some order (possibly with some photos being duplicated). A photo booklet can be described as a string of lowercase letters, consisting of the photos in the booklet in order. He now wants to sell some "special edition" photobooks, each with one extra photo inserted anywhere in the book. He wants to make as many distinct photobooks as possible, so he can make more money. He asks Haruhi, how many distinct photobooks can he make by inserting one extra photo into the photobook he already has?
Please help Haruhi solve this problem.
The first line of input will be a single string s (1 ≤ |s| ≤ 20). String s consists only of lowercase English letters.
Output a single integer equal to the number of distinct photobooks Kyoya Ootori can make.
a
51
hi
76
In the first case, we can make 'ab','ac',...,'az','ba','ca',...,'za', and 'aa', producing a total of 51 distinct photo booklets.
题意,插入一个字母,可以和给出的字母相同,求最多有多少种插法。
题解:代码分几个模块:
①求插入的字母和已知字母不同的方案数,也是最简单的。
②插入的字母和已知字母(只出现一次)相同的方案数。
③插入已知字母中多次出现(> 1)的方案数,这一种就依次取要插入的字母和上次插入的比较,不同则方案数加一。(队友小企鹅的贡献)
代码如下:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; int main() { char a[30]; int l; int num[27]; char b[30]; //重复字母 int ans; while (~scanf ("%s",a)) { memset (num,0,sizeof (num)); memset (b,'0',sizeof (b)); l = strlen (a); for (int i = 0 ; i < l ; i++) num[a[i] - 'a' + 1]++; int ant1 = 0; //多少个字母 int ant2 = 0; //多少个重复字母 for (int i = 1 ; i <= 26 ; i++) { if (num[i] == 1) ant1++; if (num[i] > 1) { ant1++; b[ant2++] = 'a' + i - 1; } } ans = (26 - ant1) * (l + 1); //插入不重复字母 ans += (ant1 - (ant2)) * l; //只出现一次的字母 //......待填坑 int sum=0,s; char ch[5]; for (int i = 0 ; i < ant2 ; i++) { s=1; ch[1] = b[i]; ch[2] = a[0]; for (int j=0;j<l;j++) { ch[3] = a[j]; ch[4] = b[i]; if (ch[3] != ch[1] || ch[2] != ch[4]) s++; //重新赋值 ch[1] = b[i]; ch[2] = a[j+1]; } sum += s; } ans += sum; printf ("%d\n",ans); } return 0; }