思路是利用两个map映射
一个用来判断是否被删除
一个用来判断当前字符串在str数组的位置
#include <bits/stdc++.h>
using namespace std;
#define MaxN 1010
string str[MaxN];
map <string,bool> used;
map <string,int> idx;
int cnt;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
for(int i=1; i<=n; i++){
string work , xxx , yyy;
cin >> work;
if( work == "touch" ){
cin >> xxx;
if( !used[xxx] ){
++cnt;
str[cnt]=xxx;
idx[xxx]=cnt;
used[xxx]=true;
}
}
else if( work == "rm" ){
cin >> xxx;
if( used[xxx] ){
used[xxx]=false;
}
}
else if( work == "rename" ){
cin >> xxx >> yyy;
if( used[xxx] && !used[yyy] ){
str[idx[xxx]]=yyy;
used[xxx]=false;
used[yyy]=true;
}
}
else{
for(int i=1; i<=cnt; i++){
if( used[str[i]] ){
cout << str[i] << '\n';
}
}
}
}
return 0;
}