CF 1368B Codeforces Subsequences

题目地址

题意:给你一个数n,构造一个字符串,使得至少有n个子串为codeforces

Solution

用贪心的思想肯定是只在codeforces基础上修改

对于每个字符,对答案的贡献都是乘以字符的个数,所以直接暴力求解即可

PS:不能直接cout,会超时

void solve()
{
    int n;cin>>n;
    int now=1;
    int flag=0;
    while(1)
    {
        int res=1;
        for(int i=1;i<=10;i++)
        {
            res*=now;
            if(res>n)
            {
                flag=1;
                break;
            }
        }
        if(flag)break;
        now++;
    }
    now--;int res=1;
    for(int i=1;i<=10;i++)res*=now;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(res>=n)break;
        res/=now;
        res*=now+1;
        cnt++;
    }
    for(int i=1;i<=10;i++)
    {
        if(cnt)
        {
            a[i]=now+1;
            cnt--;
        }else a[i]=now;
    }
    while(a[1]--)putchar('c');
    while(a[2]--)putchar('o');
    while(a[3]--)putchar('d');
    while(a[4]--)putchar('e');
    while(a[5]--)putchar('f');
    while(a[6]--)putchar('o');
    while(a[7]--)putchar('r');
    while(a[8]--)putchar('c');
    while(a[9]--)putchar('e');
    while(a[10]--)putchar('s');
}

 

posted @ 2023-03-20 15:04  HikariFears  阅读(24)  评论(0)    收藏  举报