codeforces 1554D

该博客主要讨论了一道编程竞赛题目,要求构造一个字符串,使得所有非空子序列出现次数为奇数。博主提出这是一道贪心问题,并给出了当字符串长度为奇数和偶数时的解决方案。通过将字符串分为两部分,一部分为相同字母,另一部分插入不同字母,确保每个子序列的奇偶性交替,从而达到所有子序列出现次数为奇数的目标。博主还提供了一个C++代码示例来实现这个解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Diane

  						D. Diane

You are given an integer n. Find any string s of length n consisting
only of English lowercase letters such that each non-empty substring
of s occurs in s an odd number of times. If there are multiple such
strings, output any. It can be shown that such string always exists
under the given constraints.

A string a is a substring of a string b if a can be obtained from b by
deletion of several (possibly, zero or all) characters from the
beginning and several (possibly, zero or all) characters from the end.

Input The first line contains a single integer t (1≤t≤500) — the
number of test cases.

The first line of each test case contains a single integer n
(1≤n≤105).

It is guaranteed that the sum of n over all test cases doesn’t exceed
3⋅105.

Output For each test case, print a single line containing the string
s. If there are multiple such strings, output any. It can be shown
that such string always exists under the given constraints.

题意: 给你一个字符串长度,让你求当前长度下的字符串的所有连续子序列的个数为奇数。例如: abc . a,b,c,ab,bc,abc所有连续子序列的个数都为1(奇数)

个人思路:本题绝对是一个贪心题目,且这种题目的都是有一种特别的式子能简单的得到题目的需求。

题解: 首先我们来假设一个例子 aaaaa . a(5) aa(4) aaa(3) aaaa(2) aaaaa(1)
当字符串长度为奇数时,相同的字母所组成的字符串的当长度为奇数时,数量为奇数,长度为偶数时,数量为偶数。字符串长度为偶数时则相反。那么利用他必须连续并且偶数+奇数=奇数的特性, 将当前字符串分成份,其第一份和第三分为字符串长度/2,字符串长度/2-1 中间份找到任何一个不是相同字母的单词代替(截断连续)

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10,mod=998244353,INF=0x3f3f3f3f;
int dp[N][10];

int main()
{
    int T;cin>>T;
    while(T--)
    {
        int n;cin>>n;
        if(n==1)cout<<"a"<<endl;//特判一下1的时候
        else cout<<string(n / 2,'a')+(n & 1 ? "bc":"c")+string(n/2-1,'a')<<endl;
        //简单的来一个例子 当n=9时 aaaa+bc+aaa刚好所有的连续子序列都是奇数
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值