CF1511D Min Cost String (Thinking)

Link

for the final glory.

Title:

First define if this exists

s[i]==s[j] && s[i+1]==s[j+1] (i!=j)

It will cost 1, giving the string length n and the number of characters you can choose m (m-1 counted from the character a), allowing you to construct the minimum cost string

analyze:

First of all, it is undoubtedly a construct, and secondly, we enter the topic of how to construct it. We choose the construction method: it is optimal but does not allow two consecutive characters to appear in other places, then we can construct it in a similar way to the full arrangement, assuming that there are only 5 optional characters. Then we start from a, abacadae, and then change to b to start with bcbdbe, and at the end of the cycle, we will loop from a to know that n characters are used up, but this will be WA4, why that, we will look at example 1 and find that he is aaband No ab, this caught our attention, aabso the length without cost is longer than the length abwithout cost, so it aabis better, then we just add the character of the loop in front of each loop.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x << ": " << x << endl;

const int MOD = 998244353;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const int dx[] = {
    
    0, 1, -1, 0, 0};
const int dy[] = {
    
    0, 0, 0, 1, -1};
const int dz[] = {
    
    1, -1, 0, 0, 0, 0 };
int day[] = {
    
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll n,m;
string str, s;
void solve()
{
    
    
    cin>>n>>m;
    ll l=0;
    if(m==1) {
    
    
        for(int i=0;i<n;i++)printf("a");
            return ;
    }
    while(n){
    
    
        printf("%c",char('a'+l));
            n--;
            if(!n)break;
        for(int i=l+1;i<m&&n;i++,n--){
    
    
            printf("%c",char('a'+l));
            n--;
            if(!n)break;
            printf("%c",char('a'+i));
        }
        l++;
        if(l==m) l=0;
    }
}

int main()
{
    
    
    //init();
    ll t = 1;
    //scanf("%lld", &t);
    while(t--)
    {
    
    
        solve();
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326246079&siteId=291194637