CodeForces - 1079B Personalized Cup 好题

博客围绕字符串分行补全问题展开。给定字符串,要分成x行w列(w<=20)且x最小,不够用*补。先确定行数x,通过字符长度len计算,再判断*个数,最后进行模拟分析和*号分配。

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

题意:

给一串字符串,让你分成x行w列(w<=20),保证x行最小,不够,用*补,输出x w 和字符串。

分析:

我们首先确定行数,字符长度len,x=len/x(要向上取整),判断取*的个数,(y+len)%x==0,y=x-len%x,w=len/x,然后就是模拟分析了。

然后就是分配*号,取模后 * 号的个数必然小于行数,可自行证明

#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 100+5
#define MAX 26
typedef long long ll;
int a[N],n,m;
int main()
{
	string s;
	cin>>s;
	int len=s.length();
	 if(len<=20)
	 {
	 	cout<<1<<" "<<len<<endl;
	 	cout<<s<<endl;
	 	return 0;
	 	
	 }
	// cout<<len<<endl;
	int x=len/20;   
	int y=0;
	if(x*20<len)
	{
	 x+=1;
	 if(len%x!=0)
	 y=x-len%x;
	
	}
	 // cout<<y<<endl;
	int w=(len+y)/x;
	for(int i=1;y>0;i++)
	{    y--;
		a[i]++;
	}
	cout<<x<<" "<<w<<endl;
	int k=0;
	for(int i=1;i<=x;i++)
	{ 
		
		cout<<s.substr(k,w-a[i]);
		for(int j=1;j<=a[i];j++)
		cout<<"*"<<endl;
		if(a[i]==0)
			cout<<endl;
		k+=w-a[i];
		
	}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值