CF1511D Min Cost String (思维)

链接

为了最后的荣光.

题意:

首先定义如果存在这个

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

就会花费1代价,给出字符串长度n和给出你可以选用的字符数量m(从字符a往后数m-1个),让你构造出最小花费的字符串

分析:

首先是个构造体无疑,其次我们进入正题如何构造出来我们选择构造方式:最优不过不让两个连续的字符在其他地方出现,那么我们可以类似全排列的构造出来,假设是可选字符只有5个那么我们就从a开始,abacadae,然后换b开头bcbdbe,循环到最后我们再从a循环知道n字符用完,然而这样会WA4,为什么那,我们再看看样例1会发现他是aab而不是ab这就引起我们注意了,aab这样不花费代价的长度比ab不花费代价的长度长,所以aab更优,那么我们就在每个循环前面加上循环的字符就好了。

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值