CodeForces 1506G Maximize the Remaining String 单调栈
【题目描述】
You are given a string s, consisting of lowercase Latin letters. While there is at least one character in the string s that is repeated at least twice, you perform the following operation:
you choose the index i (1≤i≤|s|) such that the character at position i occurs at least two times in the string s, and delete the character at position i, that is, replace s with s1s2…si−1si+1si+2…sn.
For example, if s=“codeforces”, then you can apply the following sequence of operations:
i=6⇒s=“codefrces”;
i=1⇒s=“odefrces”;
i=7⇒s=“odefrcs”;
Given a given string s, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string a of length n is lexicographically less than a string b of length m, if:
there is an index i (1≤i≤min(n,m)) such that the first i−1 characters of the strings a and b are the same, and the i-th character of the string a is less than i-th character of string b;
or the first min(n,m) characters in the strings a and b are the same and n<m.
For example, the string a=“aezakmi” is lexicographically less than the string b=“aezus”.
【输入】
The first line contains one integer t (1≤t≤104). Then t test cases follow.
Each test case is characterized by a string s, consisting of lowercase Latin letters (1≤|s|≤2⋅105).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed 2⋅105.
【输出】
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
【样例输入】
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
【样例输出】
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
【解题思路】
题目要求对于一个给定的字符串,删除其中重复的字母,使得原字符串中出现过的字母均只剩下一个,要求满足条件的字典序最大的字符串。
这是一道经典的单调栈问题。
我们使用一个cnt数组来记录每个字母出现的次数,变量k记录答案的长度,也即不同字母的个数。使用一个栈s来作为单调栈,从前往后遍历字符串,如果当前字母比栈顶字母大,且栈顶字母在字符串后面还存在,说明此时栈中的顺序不是最优的,我们进行相应的调整。每判断完一个字母,更新cnt数组。具体实现见代码。
【AC代码】
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
int t,cnt[30],vis[30];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--)
{
string a,ans;
cin>>a;
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
int k=0,len=a.length();
for(int i=0;i<len;i++)
cnt[a[i]-'a']++;
for(int i=0;i<26;i++)
if(cnt[i]>0)
k++;
stack<char> s;
for(int i=0;i<len;i++)
{
cnt[a[i]-'a']--;
if(vis[a[i]-'a'])
continue;
while(!s.empty()&&a[i]>s.top()&&cnt[s.top()-'a']>0)
{
vis[s.top()-'a']=0;
s.pop();
}
s.push(a[i]);
vis[a[i]-'a']=1;
}
for(int i=1;i<=k;i++)
{
ans+=s.top();
s.pop();
}
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
}
return 0;
}
2021年度训练联盟热身训练赛第一场的E题Early Orders,和这道题几乎相同,AC代码如下:
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int n,k,a[200005],cnt[200005],vis[200005],ans[100005];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>a[i];
cnt[a[i]]++;
}
stack<int> s;
for(int i=0;i<n;i++)
{
cnt[a[i]]--;
if(vis[a[i]])
continue;
while(!s.empty()&&a[i]<s.top()&&cnt[s.top()]>0)
{
vis[s.top()]=0;
s.pop();
}
s.push(a[i]);
vis[a[i]]=1;
}
for(int i=k;i>=1;i--)
{
ans[i]=s.top();
s.pop();
}
for(int i=1;i<=k;i++)
cout<<ans[i]<<" ";
cout<<endl;
return 0;
}