跳到正文

题解

题解:P17287 [Algo Beat Contest 013 & MSOI R2] 付之一笑

dy_yyz约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年8月22日
收录
2026年8月22日
更新
2026年8月22日
篇幅
约 2 分钟
题目
P17287

版本

  1. v1当前

前言:

刚刚打完比赛就来写题解啦~

思路:

纯模拟,只需将每个字符串进行遍历即可,符合条件直接输出,不符合则进行处理。

注意不要写反了!

AC代码:

C++
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int q;
	cin>>q;
	for(int i=1;i<=q;i++)
	{
		int p;
		cin>>p>>s;
		int l=s.length();
		if(p==1)
		{
            int j=0;
			while(j<l)
			{
				if(s[j]>='0'&&s[j]<='9')
				{
					int k=(int)(s[j]-'0');
					j++;
					while(s[j]>='0'&&s[j]<='9')
					{
						k=k*10+(int)(s[j]-'0'); // 注意要乘 10 
						j++; // 累加本身效率更高 
					}
					cout<<(char)(k+'a'-1); //减 1 不要写错 
				}
				else
                {
                    cout<<s[j];
                    j++;
                }
			}
		}
		else
		{
			for(int j=0;j<l;j++)
			{
				if(s[j]>='0'&&s[j]<='9') cout<<s[j];
				else cout<<(int)(s[j]-'a'+1);
			}
		} // 数字串较为简单,不进行解释 
		cout<<endl; // 换行也不要忘了写 
	}
	return 0;
}

这样就可以做到 O(∣s∣)O(\lvert s \rvert) 的时间复杂度,不会超时。

恭喜你又看完了一篇题解!

原文