题目
题解思路
运用双指针,对两边的字符大小做比较,拿小的,当发现他们相等时不能乱拿,要扫一遍看看之后有没有更小的在一边,如何有就拿那一边的。这样就能保证字典序最小。
输出要注意了,80个字符要换行。
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
string s,t;
int check(int l,int r)
{
while(l <= r )
{
if (s[l] < s[r] )
return 1;
else if (s[l] > s[r] )
return 0;
else
{
l++;
r--;
}
}
return 2;
}
int main ()
{
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin>>n;
for (int i = 0 ; i < n ; i++ )
{
cin>>t;
s += t;
}
int l = 0 , r = n-1;
t = "";
while(l <= r )
{
if (s[l] > s[r] )
{
t += s[r];
r--;
} else if (s[l] == s[r] )
{
int k = check(l,r);
if (k)
{
t += s[l];
l++;
}else
{
t += s[r];
r--;
}
}
else
{
t += s[l];
l++;
}
}
for (int i = 0 ; i < t.size() ; i++ )
{
cout<<t[i];
if ((i+1)%80 == 0)
cout<<"\n";
}
return 0;
}