码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 591B Rebranding (模拟)

时间:2017-03-09 22:58:06      阅读:420      评论:0      收藏:0      [点我收藏+]

标签:letters   osi   rpo   std   span   des   spi   string   ssi   

Rebranding

Problem Description

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 xi by 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 letters xi and yi.

Output

Print the new name of the corporation.

Examples Input

11 6
abacabadaba
a b
b c
a d
e g
f a
b b

Examples Output

cdcbcdcfcdc

题目链接:http://codeforces.com/problemset/problem/591/B


题意:先输入 1<m,n<200000,m为原字符串的长度,n为变换规则的次数。再input长度为m的字符串。接下来输入n对(c1 c2)变换规则:每次变换规则(上次变换后的字符串的 c1变成c2 c2变成c1);

思路一:炸了

第一个想到的肯定是循环n次,每次都对上一个字符串中的 c1 c2进行变换。

每次变换需要循环m次去查找有没有等于c1,c2 的字符。

那么这个暴力模拟的时间复杂度就为O(n*m) o.o... 题目时间限制是 2000ms  而 m n 都是最大20完的数,所以肯定要炸喽...

思路二:

总体来想,假如原来的名字就是一个字符,经过变换最终到另外一个字符。

也就是说一个字符通过一系列会到一个确定的字符,而一个字符串就好比多个单个字符排在一起而已。

所以我们只需要模拟26个英文字母 经过一系列变换 最终变成了什么就可以了。


AC代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int MAXN=200000+100;
 5 char c[27]="abcdefghijklmnopqrstuvwxyz";
 6 char ss[MAXN]={0};
 7 int main()
 8 {//a 97 b 98
 9     int n,m;
10     char c1,c2;
11     scanf("%d%d%*c%s%*c",&n,&m,ss);
12     for(int i=0;i<m;i++)
13     {
14         scanf("%c%*c%c%*c",&c1,&c2);
15         for(int j=0;j<26;j++)
16         {
17             if(c[j]==c1)
18                 c[j]=c2;
19             else if(c[j]==c2)
20                 c[j]=c1;
21         }
22     }
23     for(int i=0;i<n;i++)
24         printf("%c",c[ss[i]-97]);
25     cout<<endl;
26     return 0;
27 }

2017-03-09 22:03:35

codeforces 591B Rebranding (模拟)

标签:letters   osi   rpo   std   span   des   spi   string   ssi   

原文地址:http://www.cnblogs.com/Twobox/p/6528158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!