1. 程式人生 > >CodeForces - 848A From Y to Y (找規律)

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

nbsp 得到 串合並 memset include scan ret spa 可能

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

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

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

AC代碼:

 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 }

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