CodeForces - 848A From Y to Y (找規律)

http://codeforces.com/problemset/problem/848/Aios

 

題目大意:剛開始集合裏面都是單字符可認爲是字符串,而後讓你去合併任意兩個串合併要消耗∑c=(a~z) f ( s , c ) * f ( t , c ) 的能量,其中 f ( s , c )表示字符串s中單字符c的個數。如今已知把全部字符合成一行字符串要花費的能量,問這個字符串可能的是?c++

 

解題思路:當 t 是和 s 中字符都相同的單字符時s和t組合的結果就是s的長度,對於一種字符來講增長的能量就是 l*(l+1)/2。咱們從a開始枚舉當前l的最大就可以獲得最終的結果了,即每種字符所用的個數。spa

 

AC代碼:code

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,a[30];
 7     scanf("%d",&n);
 8     memset(a,0,sizeof(a));
 9     for(int  i=0; i<26; i++)
10     {
11         for(int j=400; j>=0; j--)
12         {
13             if(n>=j*(j+1)/2)
14             {
15                 a[i]=j+1;
16                 n=n-j*(j+1)/2;
17                 break;
18             }
19         }
20         if(!n)
21             break;
22     }
23     for(int i=0;i<26;i++)
24     {
25         while(a[i])
26         {
27             printf("%c",i+'a');
28             a[i]--;
29         }
30     }
31     printf("\n");
32     return 0;
33 }
相關文章
相關標籤/搜索