【Codeforces Round #327 (Div. 2) B】 Rebranding 字符变换 同类型打标记

本文概述了AI音视频处理领域的关键技术,包括视频分割、语义识别、自动驾驶、AR、SLAM等,旨在为读者提供全面的了解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Rebranding
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.

For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xiby yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation.

Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive.

Satisfy Arkady's curiosity and tell him the final version of the name.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively.

The second line consists of n lowercase English letters and represents the original name of the corporation.

Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English lettersxi and yi.

Output

Print the new name of the corporation.

Sample test(s)
input
6 1
police
p m
output
molice
input
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
output
cdcbcdcfcdc
Note

In the second sample the name of the corporation consecutively changes as follows:


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=2e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n,m;
char s[N];
char a[128],x,y;//a[i]表示一开始是i的字符最后变成了什么
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i='a';i<='z';i++)a[i]=i;
		scanf("%s",s);
		for(int i=1;i<=m;i++)
		{
			scanf(" %c %c",&x,&y);
			for(int i='a';i<='z';i++)
			{
				if(a[i]==x)a[i]=y;
				else if(a[i]==y)a[i]=x;
			}
		}
		for(int i=0;s[i];i++)s[i]=a[s[i]];
		puts(s);
	}
	return 0;
}
/*
【题意】
给你一个长度为n(2e5)的串,串中只包含小写字母。
我们先后进行了m(2e5)次替代,
对于每次替代规则(x,y),把当前为x的字符变成字符y,并把当前为y的字符变成字符x。
问你经过这m次替代之后,串变成了什么模样并输出。

【类型】
暴力

【分析】
显然不能模拟。
首先观察到,一开始是某个char类型的,最后都会变成相同的char类型。
于是我们需要用a[x]表示初始字符是x,最终变成的字符。
这样,对于每个替代规则(x,y),我们扫描一下a['a'~'z'],把a[i]==x的变为a[i]=y,把a[i]==y的变为a[i]=x。
就可以AC啦。
字符集果然是简化复杂度的一大重要突破口。

【时间复杂度&&优化】
O(26m)

【数据】
Input
6 1
police
p m
Output
molice

Input
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
Output
cdcbcdcfcdc

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值