唐氏若智求助
  • 板块学术版
  • 楼主vectorxyz
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/6 16:07
  • 上次更新2024/7/6 16:37:27
查看原帖
唐氏若智求助
1114241
vectorxyz楼主2024/7/6 16:07

P6140

30pts

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5;
char s[N];

int main()
{
	string ans = "";
	int n;
	cin >> n;
	
	for(int i = 0;i < n;i ++ ) {
		cin >> s[i];
	}
	
	int i = 0, j = n - 1;
	while(i < j)
	{
		if(s[i] < s[j]) {
			ans += s[i];
			i ++ ;
		}
		else if(s[i] > s[j]){
			ans += s[j];
			j -- ;
		}
		else {
//		cout << i << ' ' << j << endl;
			int lx = 0;
			for(lx = 1; ; lx ++ )
			{
				if(s[i + lx] != s[j - lx]) break;
			}
//			cout << s[j - lx] << endl;
			if(s[i + lx] < s[j - lx])
			{
				for(int k = 0;k <= lx;k ++ ) {
					ans += s[i + k];
				}
				i += (lx + 1);
			}
			if(s[i + lx] > s[j - lx])
			{
				for(int k = 0;k <= lx;k ++ ) {
					ans += s[j - k];
				}
				j -= (lx + 1);
			}
		}
	}
	ans += s[i];
//	cout << ans.size() << endl;
	for(int i = 0;i < n;i ++ ) {
		cout << ans[i];
		if((i + 1) % 80 == 0) {
			puts("");
		}
	}
	return 0;
}
2024/7/6 16:07
加载中...