2019-2020 ICPC SEERC D - Cycle String?(思维+模拟)

10 篇文章 0 订阅
4 篇文章 0 订阅

传送门

题意:

给定长度为 2 ∗ n 2*n 2n的循环字符串,对其重新排列使得新字符串中不存在两个及以上长度为 n n n的相同子串,问能否构造成功并输出任意新字符串。

思路:

首先容易发现新串是否合法与出现最多字符的数量有关,稍微打个表发现:出现次数最多的字符(一下称 c h ch ch)出现了 x x x次.
① ① 如果 x < = n x <= n x<=n,那么我们直接输出原串就好了。
② ② 如果字符种类数 > = 3 >=3 >=3,那么一定有解(就是间隔构造)
③ ③ 如果 x > = 2 ∗ n − 2 x >= 2*n-2 x>=2n2,并且字符种类数 = = 2 ==2 ==2,那么一定无解
④ ④ 如果 x < 2 ∗ n − 2 x < 2*n-2 x<2n2,并且字符种类数 = = 2 ==2 ==2,那么可以先构造出 n n n c h ch ch,然后插一个其他字符,再扔进剩下的 c h ch ch,再扔进剩下的其他字符,这样就保证了不会出现连续的大于 n n n个相同的字符。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6+5;

char s[maxn];

set<char>st;
int num[30];
char t1[maxn],t2[maxn];
int main(){
	scanf("%s",s+1);
	int len = strlen(s+1);
	int n = len/2;
	for(int i = 1; i <= len; i++){
		st.insert(s[i]);
		num[s[i]-'a']++;
	}
	if(st.size() <= 1) puts("NO");
	else if(st.size() >= 2){
		int mx = 0,val = 0;
		for(int i = 0; i < 26; i++){
			if(num[i]) val++;
			mx = max(mx, num[i]);
		}
		
		int cnt1 = 0, cnt2 = 0;
		for(int i = 0; i < 26; i++){
			if(num[i] == 0) continue;
			if(num[i] == mx) for(int j = 0; j < num[i]; j++) t1[cnt1++] = i+'a';
			else  for(int j = 0; j < num[i]; j++) t2[cnt2++] = i+'a';
		}
		if(st.size() == 2){
			if(mx <= n){
				puts("YES");
				sort(s+1,s+len+1);
				puts(s+1);
			}
			else if(mx >= 2*n-2) puts("NO");
			else{
				puts("YES");
				int c1 = 0, c2 = 0;
				for(int i = 0; i < min(n, mx); i++) printf("%c",t1[i]);
				c1 = min(n,mx);
				if(cnt2){
					printf("%c",t2[0]);
					c2 = 1;	
				}
				for(int i = c1; i < cnt1; i++) printf("%c",t1[i]);
				for(int i = c2; i < cnt2; i++) printf("%c",t2[i]);
			}
		}else{
			puts("YES");
			int c1 = 0, c2 = 0;
			for(int i = 0; i < min(n, mx); i++) printf("%c",t1[i]);
			c1 = min(n,mx);
			if(cnt2){
				printf("%c",t2[0]);
				c2 = 1;	
			}
			for(int i = c1; i < cnt1; i++) printf("%c",t1[i]);
			for(int i = c2; i < cnt2; i++) printf("%c",t2[i]);
		}
	}
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值