标签:read bae creat namespace ice logo 题目 code 建立
个人心得:题目意思就是每次给出可以互换的字母,如果每次命令的时候就执行的话一定会超时。
所以我就是将输入的字母从a到z的数目和路径依次保存,再建立一个book数组表示字母现在所指的字母
,一开始就直接数组转换结果超时了,有一点并查集的思想。
就比如说 a b转换,a 此时有3个,分别为1,3,5。b有2个,分别为2,6
则此时a 应该指向b的二维数组,而b就指向a的数组。
1 while(m--) 2 { 3 cin>>t1>>t2; 4 if(t1!=t2) 5 { 6 int x=t1-‘a‘; 7 int y=t2-‘a‘; 8 int t=zimu[x]; 9 zimu[x]=zimu[y]; 10 zimu[y]=t; 11 }
后面就根据对应的路径扔到原来的char数组里就好了
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.
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 xiand yi.
Print the new name of the corporation.
6 1
police
p m
molice
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
cdcbcdcfcdc
In the second sample the name of the corporation consecutively changes as follows:
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<set> 7 using namespace std; 8 char text[200005]; 9 int point[30][1000000]; 10 int zimu[26]; 11 int main() 12 { 13 14 int n,m; 15 cin>>n>>m; 16 cin>>text; 17 for(int i=0;i<26;i++) 18 zimu[i]=i; 19 int t; 20 for(int i=0;i<n;i++) 21 { 22 t=text[i]-‘a‘; 23 point[t][0]++; 24 int p=point[t][0]; 25 point[t][p]=i; 26 27 } 28 char t1,t2; 29 while(m--) 30 { 31 cin>>t1>>t2; 32 if(t1!=t2) 33 { 34 int x=t1-‘a‘; 35 int y=t2-‘a‘; 36 int t=zimu[x]; 37 zimu[x]=zimu[y]; 38 zimu[y]=t; 39 } 40 41 } 42 for(int i=0;i<26;i++) 43 { 44 int t=zimu[i]; 45 char mmp=i+97; 46 if(point[t][0]==0) continue; 47 for(int j=1;j<=point[t][0];j++) 48 { 49 int p=point[t][j]; 50 text[p]=mmp; 51 } 52 } 53 cout<<text<<endl; 54 55 return 0; 56 57 58 }
标签:read bae creat namespace ice logo 题目 code 建立
原文地址:http://www.cnblogs.com/blvt/p/7286719.html