题意:给一个费用k,输出一个字符串,合并这个字符串的最小费用为k,费用为:一个字母在两个字符串中出现的次数的乘的累加和。
思路:不同字母合并不需要费用,只含一个字母的字符串长度为c,我们发现它的费用为c*(c-1)/2。总费用就是不同长度含不同字母的字符串的费用和,每次给当前字母长度+1,超过费用,就回到上一次位置,换一个字母继续。费用为0,需要特判。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+5;
char ans[maxn];
int tot;
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
tot = 0;
int sum = 0,res = 0,last = 0,now = 0;
while(sum != n)
{
now++;
if(now >= 2)
last = now*(now-1)/2 - (now-2)*(now-1)/2;
else
last = now*(now-1)/2;
sum += last;
ans[tot++] = 'a' + res;
if(sum > n)
{
res++;
tot--;
sum -= last;
last = 0;
now = 0;
}
}
ans[tot] = '\0';
if(n != 0)
printf("%s\n",ans);
else
printf("a\n");
}
return 0;
}