codeforces 591B Rebranding (模拟)

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

时间: 2024-12-23 19:55:04

codeforces 591B Rebranding (模拟)的相关文章

CodeForces 591B Rebranding

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 th

Codeforces Round #327 (Div. 2) B. Rebranding 模拟

B. Rebranding 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

Codeforces 389B(十字模拟)

Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Fox Ciel has a board with n rows and n columns. So, the board consists of n × n cells. Each cell contains either a symbol '.', or a s

CodeForces 697B Barnicle 模拟

强行模拟 纪念一下…… 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<string.h> 6 #include<string> 7 #include<map> 8 #include<vector> 9 #include<queue> 10 #define M(a

CodeForces - 344A Magnets (模拟题)

CodeForces - 344A Magnets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangula

CodeForces 670E(模拟双向链表)

题意:给你一串合法的括号和当前光标的位置和一些操作,问操作完之后的串是怎么样的 思路:模拟一个双向链表的操作,首先先预处理出配对的括号组,然后模拟即可 #include<bits\stdc++.h> using namespace std; const int maxn = 1e6; struct Node { int l,r; }nodes[maxn]; char s1[maxn],s2[maxn]; int a[maxn],d[maxn]; int main() { int n,m,pos

CodeForces 709B Checkpoints 模拟

题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stdio.h> 6 #include<stdlib.h> 7 #inc

Berland National Library codeforces 567B(模拟)

http://codeforces.com/problemset/problem/567/B 题意:图书馆有一个程序,可以记录学生的进出情况,'+'表示进入,'-'表示出去,字符后面的数字表示学生的编号.在这个程序尚未启动前,还有一些同学进的消息没有被记录下来.现在问你这个图书馆至少得容纳多少学生. 分析:用sum和ans两个值来记录,sum存当前房间人数,ans维护最大值. #include <iostream> #include <stdio.h> #include <s

CodeForces 591B

题目链接: http://codeforces.com/problemset/problem/591/B 题意: 给你一串字符串,字符串里的字符全是a-z的小写字母,下面的m行,是字符串的交换方式, 如果输入的是 x  y,则是字母x与字母y交换,且字母y与字母x也要交换 解题思路: n和m的范围在2*10^5内,如果直接暴力肯定会超时: 所以只能采取智慧的方法 可以先定义 char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','