跳到正文

题解

题解:P13189 [GCJ 2016 #1A] The Last Word

AC_zzh2013约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年2月4日
更新
2026年2月4日
篇幅
约 1 分钟
题目
P13189

版本

  1. v1当前

题意

题目给出一个字符串,让我们按字符串顺序将当前字符拼在新字符串的前面或后面,最后找到字典序最大的一个字符串。

思路

既然要找最大的,那么我们就可以在拼新字符串时作分类讨论,只要当前字符比新字符串的第 00 位大,那么就把他拼在前面,否则拼在后面。

代码

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	int cnt=0;
	while(t--)
	{
		cnt++;
		string s;
		cin>>s;
		int len=s.size();
		string ans="";
		for(int i=0;i<len;++i)
		{
			if(s[i]>=ans[0])
			{
				ans=s[i]+ans;
			}
			else
			{
				ans=ans+s[i];
			}
		}
		cout<<"Case #"<<cnt<<": "<<ans<<endl;
	}
	return 0;
}
原文