题目链接:http://codeforces.com/problemset/problem/591/B
题 意:给你一个字符数组级数组内字母的变化方式,问变化后的数组是什么。
题 意:不可暴力,会超时。
要将26个字母用1~26编号并存入数组,再将每两个要交换的字母的对应编号交换,最后将每个字母变换后的字母存入另一个数组,再在这个数组内找到要求的字符数组的每一个字符变换后的字母并输出。
代码如下:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stack>
using namespace std;
typedef __int64 LL;
char str[200006];
int main()
{
int l, n;
while( scanf ( "%d %d", &l, &n ) != EOF )
{
scanf ( "%s", str );
int a[26], b[26];
for( int i = 0; i < 26; i ++ )
a[i] = i;//开始时每一个字母对应的编号a~z
for( int i = 0; i < n; i ++ )
{
char x, y;
scanf ( " %c %c", &x, &y );
int xx = x - 'a';
int yy = y - 'a';
int t = a[xx];//编号交换
a[xx] = a[yy];
a[yy] = t;
}
for( int i = 0; i < 26; i ++ )
b[ a[i] ] = i;//a[i]即是第i个字母变换后对应的字母
for( int i = 0; i < l; i ++ )
{
str[i] = b[ str[i] - 'a' ] + 'a';//找对应的字母
}
puts(str);
}
return 0;
}