A. Digits Sequence Dividing---Educational Codeforces Round 59 (Rated for Div. 2)

本文详细解析了Codeforces 1107A题目的解题思路,介绍了如何通过字符串操作来解决数字序列划分问题,提供了一段C++代码示例,演示了如何判断并划分字符串,确保每部分满足递增条件。

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

Digits Sequence Dividing

time limit per tes 1 second
memory limit per test 256 megabytes

题目链接http://codeforces.com/problemset/problem/1107/A

在这里插入图片描述
在这里插入图片描述


题目大意:给你n个询问,问你是否能将一个数分成n段,对于每段i<j,ti<tj,如果能输出YES,并输出其划分段数和划分的内容。

emmmm,就是个字符串的操作,不过这题用string写起来比较放便,我们可以先取第一个数为第一段,只有当n=2,且第二个数<=第一个数的时候是NO的。接下来我们直接模拟,每次加上一个数之后判断是否大于前面的数,如果大于则划分下一段,否则继续添加数。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;
const int mac=2e5+10;
#define ll long long 
int a[mac];
int judge(string s1,string s2)
{
	if (s1.size()>s2.size()) return 0;
	if (s1.size()<s2.size()) return 1;
	else {
		if (s1<=s2) return 1;
		else return 0;
	}
}
int main()
{
	int q,n;
	scanf ("%d",&q);
	while (q--){
	    string s,ans[310];
		scanf ("%d",&n);
		cin>>s;
		int num=1;
		if (s.size()==2 && s[0]>=s[1]) {
			printf ("NO\n");
			continue;
		}
		else {
			ans[0]=s[0];
			for (int i=1; i<s.size(); i++){
				if (judge(ans[num],ans[num-1]))
				  ans[num]+=s[i];
				else ans[++num]=s[i];
			}
		}
		if (judge(ans[num],ans[num-1])) ans[num-1]+=ans[num],num--;
		cout<<"YES"<<endl;
		cout<<num+1<<endl;
		for (int i=0; i<=num; i++){
			cout<<ans[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值