PAT 1602. 卡住的键盘(unordered_map重新认识)
作者:
cyuyu
,
2022-08-10 09:28:27
,
所有人可见
,
阅读 177
之前对于unordered_map<>认为我存入的顺序是怎样的输出顺序就是怎样的,但是经过本题目,发现其实并不是这个样子的#,他在内部打乱了输入的顺序,所以按序对unordered_map<>输出的顺序是不确定性的!!!
#include<iostream>
#include<unordered_map>
using namespace std;
const int N=1010;
unordered_map<char,int>res,st;
int main(){
int k;
cin>>k;
string s;
cin>>s;
int j=0;
string end="";
for(int i=0;i<s.size();i=j){
int temp=0;
j=i+1;
temp++;
while(s[i]==s[j]&&j<s.size()){
temp++;
j++;
}
if(temp%k==0&&temp>=k){
if(!res.count(s[i]))
res[s[i]]=1;
}
else
{
res[s[i]]=0;
}
}
for(int i=1;i<s.size();i++){---------->要想按照插入顺序输出,必须得这样!!!
if(res[s[i]]==1&&!st.count(s[i])){
cout<<s[i];
st[s[i]]==1;
}
}
cout<<endl;
int p=0;
for(int i=0;i<s.size();i=j){
int temp=0;
j=i+1;
temp++;
while(s[i]==s[j]&&j<s.size()){
temp++;
j++;
}
if(temp>=k&&res[s[i]]==1){
end.insert(p,temp/k,s[i]);
p+=temp/k;
}
else{
end.insert(p,temp,s[i]);
p+=temp;
}
}
cout<<end;
return 0;
}