append 函数 _构造算法 _1927_B. Following the String

cpp 复制代码
#include<bits/stdc++.h>

using namespace std;

const int N=2e5+10;

int a[N];
int cnt[30];
char s[N];

void solve()
{
	int n;
	cin>>n;
	
	for(int i=0;i<n;i++)
		cin>>a[i];
	int q=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]==0)
		{
			s[i]='a'+q;
			q++;
			cnt[s[i]-'a']++;
		}
		else
		{
			for(int j=0;j<26;j++)
				if(cnt[j]==a[i])
				{
					s[i]=j+'a';
					break;
				}
			cnt[s[i]-'a']++;
		}
	}
	
	for(int i=0;i<n;i++)
		cout<<s[i];
	cout<<endl;
	
	memset(cnt,0,sizeof cnt);
	memset(s,0,sizeof s);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin>>t;
	
	while(t--)
		solve();
	
	return 0;
}

赛时错了一次,自己能写出来纯属运气,不知道咋处理其实

感觉还是比较复杂,输入的是出现次数,这里的出现次数指的是,该下标左边该字母的出现次数

答案不唯一,从 a 开始计算就好了

下面贴一下我之前的错误代码

cpp 复制代码
#include<bits/stdc++.h>

using namespace std;

const int N=2e5+10;

int a[N];
int cnt[30];
char s[N];

void solve()
{
	int n;
	cin>>n;
	
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=0;i<n;i++)
	{
		if(a[i]==0)
		{
			s[i]='a'+a[i]+i;
			cnt[s[i]-'a']++;
		}
		else
		{
			for(int j=0;j<26;j++)
				if(cnt[j]==a[i])
				{
					s[i]=j+'a';
					break;
				}
			cnt[s[i]-'a']++;
		}
	}
	
	for(int i=0;i<n;i++)
		cout<<s[i];
	cout<<endl;
	
	memset(cnt,0,sizeof cnt);
	memset(s,0,sizeof s);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin>>t;
	
	while(t--)
		solve();
	
	return 0;
}

这个代码错是因为跳过了一个字母,可能就无法表示全所有的情况

还是来说正确代码的思路,a 数组输入的是出现次数,cnt 数组表示的是该字母的出现次数,出现次数相等的时候

看了一下题解,找到了一个比较优雅的做法

cpp 复制代码
#include<bits/stdc++.h>

using namespace std;

//const int N=2e5+10;

//int a[N];
//int cnt[26];

//void solve()
//{
//	int n;
//	cin>>n;
//	
//	for(int i=0;i<26;i++)
//		cnt[i]=0;
//	
//	string ans;
//	
//	for(int i=0;i<n;i++)
//	{
//		for(int j=0;j<26;j++)
//			if(cnt[j]==a[i])
//			{
//				ans.append(1,'a'+j);
//				cnt[j]++;
//				break;
//			}
//	}
//
//	cout<<ans<<endl;
//}

//void solve() {
//	int n,x;cin>>n;
//	int cnt[26] = {0};
//	string ans;
//	for(int i = 1;i<=n;i++)
//	{
//		cin>>x;
//		for(int j = 0;j<26;j++)
//		{
//			if(cnt[j]==x)
//			{
//				ans.append(1,j+'a');
//				cnt[j]++;
//				break;
//			}
//		}
//	}
//	cout<<ans<<'\n';
//}

//void solve()
//{
//	int n;
//	cin>>n;
//	
//	int cnt[26]={0};
//	
//	string ans;
//	
//	for(int i=0;i<n;i++)
//	{
//		int x;
//		cin>>x;
//		
//		for(int j=0;j<26;j++)
//		{
//			if(cnt[j]==x)
//			{
//				ans.append(1,'a'+j);
//				cnt[j]++;
//				break;
//			}
//		}
//	}
//	
//	cout<<ans<<endl;
//}

void solve()
{
	int n;
	cin>>n;
	
	int cnt[26]={0};
	
	string ans;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		
		for(int j=0;j<26;j++)
			if(cnt[j]==x)
			{
				ans.append(1,'a'+j);
				cnt[j]++;
				break;
			}
	}
	
	cout<<ans<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin>>t;
	
	while(t--)
		solve();
	
	return 0;
}

其实就只有一个条件判断就行了

就是字母的出现次数和输入的元素刚好相等,然后把字符串读入,更新字母出现次数,跳出循环,append 函数是添加字符到字符串后面,第一个参数表示添加的字符个数

应该还是比较常规的题,更快写出来才是王道

还有就是写题可以用一份代码,只是把之前的函数注释掉即可(我考试的时候就是这么干的)

相关推荐
clock的时钟12 分钟前
暑期数据结构第一天
数据结构·算法
小小小小王王王36 分钟前
求猪肉价格最大值
数据结构·c++·算法
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
BIYing_Aurora1 小时前
【IPMV】图像处理与机器视觉:Lec13 Robust Estimation with RANSAC
图像处理·人工智能·算法·计算机视觉
martian6653 小时前
支持向量机(SVM)深度解析:从数学根基到工程实践
算法·机器学习·支持向量机
孟大本事要学习3 小时前
算法19天|回溯算法:理论基础、组合、组合总和Ⅲ、电话号码的字母组合
算法
??tobenewyorker3 小时前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
让我们一起加油好吗4 小时前
【基础算法】贪心 (二) :推公式
数据结构·数学·算法·贪心算法·洛谷
贾全4 小时前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天4 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票