SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphabet tiles. Abhishar wrote a string using tiles and gave a set of pairs (i,j) to Taplu. Pair “i, j” (0 based indexing) means that Taplu can swap the i’th and j’th tile in the string any number of times. He then asks Taplu to give the lexicographically smallest string that can be produced by doing any number of swaps on the original string.
Input
First line contains T(1<=T<=10), the number of testcases.
First line of each test case contains the initial string S. Length of Sttring is len(1<=len<=100000).
Next line contains the number of pairs M (1<=M<=100000).
Next M lines contains pairs i j that means ith character can be swapped with jth character.
Note - i and j can be same and same i,j pair can occur twice.
Output
For each testcase output the lexicographically smallest string that can be made from the initial string.
Example
- Input:
- 1
lmaf
3
0 1
1 2
2 3- Output:
- aflm
题意:给定字符串S,以及M对关系(i,j),表示i位置和j位置上的字符可以交换任意次。现在让你交换,得到最小字典序的S。
思路:我们把有()关系的i,j对放到一个并查集里,不难证明,一个并查集内的任意两个位置是可以互换的,所以我们把分别把所有并查集排序即可。
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn=;
- char c[maxn],ans[maxn];
- int fa[maxn],group[maxn],tot;
- vector<int>G[maxn];
- struct in { int id; }s[maxn]; int num;
- bool cmp(in w,in v){ return c[w.id]<c[v.id]; }
- int find(int x){
- if(x==fa[x]) return x;
- fa[x]=find(fa[x]);
- return fa[x];
- }
- int main()
- {
- int T,N,M,a,b,faa,fab,i,j;
- scanf("%d",&T);
- while(T--){
- scanf("%s%d",c+,&M);
- tot=; N=strlen(c+);
- memset(group,,sizeof(group));
- for(i=;i<=N;i++) fa[i]=i;
- for(i=;i<=M;i++){
- scanf("%d%d",&a,&b);
- faa=find(a+); fab=find(b+);
- if(faa!=fab) fa[faa]=fab;
- }
- for(i=;i<=N;i++){
- faa=find(i);
- if(!group[faa]){
- group[faa]=++tot;
- G[tot].clear();
- }
- G[group[faa]].push_back(i);
- }
- for(i=;i<=tot;i++){
- num=;
- for(j=;j<G[i].size();j++) s[++num].id=G[i][j];
- sort(s+,s+num+,cmp);
- for(j=;j<G[i].size();j++) ans[G[i][j]]=c[s[j+].id];
- }
- for(i=;i<=N;i++) printf("%c",ans[i]); printf("\n");
- }
- return ;
- }
SPOJ:Lexicographically Smallest(并查集&排序)的更多相关文章
- FZU 2059 MM (并查集+排序插入)
Problem 2059 MM Accept: 109 Submit: 484Time Limit: 1000 mSec Memory Limit : 32768 KB Problem ...
- SPOJ IAPCR2F 【并查集】
思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...
- SPOJ LEXSTR 并查集
题目描述: Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
- hdu 1811Rank of Tetris (并查集 + 拓扑排序)
/* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 并查集+拓扑排序 赛码 1009 Exploration
题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...
- LA 4255 (拓扑排序 并查集) Guess
设这个序列的前缀和为Si(0 <= i <= n),S0 = 0 每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下. 还要注意一下前缀和相等的情况,所以用一个并查集来查询. ...
- Rank of Tetris(hdu1811拓扑排序+并查集)
题意:关于Rating的信息.这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rati ...
随机推荐
- SGU 107 数学题
题意:求平方后末尾9个数是987654321的数个数. 之前做此题,竟然愚蠢到用计算器 在哪里算,还加笔算,SB啊!不知道先打印一下吗! #include<iostream> #inclu ...
- 使用nginx时,让web取得原始请求地址
问题描述 当使用nginx配置proxy_pass参数以后,后端web取得的Request.Uri是proxy_pass中配置的地址,而不是client访问的原始地址 举例说明: 假设nginx配置文 ...
- (9)C#连mysql
1官网下载 dll 2. using MySql.Data.MySqlClient; 3. <add key="con_MES" value="server=192 ...
- CentOS6、CentOS7配置Base源和epel源
1.用yum安装软件报错 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&a ...
- 应用程序中的server错误,没有名称为“ServiceBehavior”的服务行为
应用程序中的server错误,没有名称为"ServiceBehavior"的服务行为 今天在阅读"创建和使用Web服务"的相关内容,在浏览器中查 ...
- 安装ftp服务器
Linux安装ftp组件 1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf文件,是vsftp的配置文件. [root@bogon ~]# yum -y insta ...
- Service具体解释(一):什么是Service
< Service具体解释(一):什么是Service> < Service具体解释(二):Service生命周期> <Service具体解释(三):Service的使用 ...
- Android 系统广播机制
一.Android应用程序注冊广播接收器(registerReceiver)的过程分析 參考Android应用程序注冊广播接收器(registerReceiver)的过程分析http://blog.c ...
- SQL数据库 更改数据类型
向表中添加数据 alter table 表名 add 列名 类型 更改表中列的数据类型 alter table 表名 alter column 列名 类型 删除表中的指定列 alter table 表 ...
- ShopMall
https://github.com/KingsleyYau/ShopMall-Android https://github.com/KingsleyYau/ShopMall-iOS https:// ...