做法:
- 用vector 把每个字母的相应下标保存好,然后swap交换即可
- 然后自己忘记了swap可以交换容器的迭代器,自己应用结构体写了一个类似vector的东西,来实现交换,结果时间被卡。
- 一查,发现swap交换迭代器的复杂度非常低,它是直接交换数据所在的内存空间地址,而不是交换数据,节省了空间消耗和时间
- _(:з」∠)_
AC代码:
#include<bits/stdc++.h>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x) push_back(x)
#define sz(x) (int)(x).size()
#define sc(x) scanf("%d",&x)
#define pr(x) printf("%d\n",x)
#define abs(x) ((x)<0 ? -(x) : x)
#define all(x) x.begin(),x.end()
#define mk(x,y) make_pair(x,y)
#define debug printf("!!!!!!\n")
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e6;
const int maxn = 2e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
vector<int> vec[150];
char s[maxn];
char ans[maxn];
int main()
{
//fin;
IO;
int n,m;
cin>>n>>m;
cin>>s;
for(int i=0;s[i];i++) vec[s[i]].pb(i);
for(int i=0;i<m;i++)
{
char x,y;
cin>>x>>y;
swap(vec[x],vec[y]);
}
for(char i = 'a';i<='z';i++)
{
for(int j=0;j<(int)vec[i].size();j++)
ans[vec[i][j]] = i;
}
cout<<ans<<endl;
return 0;
}