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 ...
随机推荐
- MongoDB增删改查操作详解(命令行)
一.插入 MongoDB的插入操作很简单,使用insert方法,这里演示从创建数据库.创建集合到插入文档.查询文档. 集合创建方法参数说明: size:集合最大空间 max:集合最多文档数量 (超出s ...
- 关于MySQL的事务处理及隔离级别
原文地址 :http://blog.sina.com.cn/s/blog_4c197d420101awhc.html 事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序 ...
- BZOJ——1611: [Usaco2008 Feb]Meteor Shower流星雨
http://www.lydsy.com/JudgeOnline/problem.php?id=1611 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
- T1245 最小的N个和 codevs
http://codevs.cn/problem/1245/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有两个长度 ...
- Leetcode 数组问题:删除排序数组内的重复项
问题描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- emacs 下 common lisp 配置
安装 sbcl .emacs 加入 ;for lisp mode (add-to-list 'load-path "D:/kuaipan/.emacs.d/elpa/slime-201311 ...
- Mysql 性能优化20个原则(3)
12. Prepared Statements Prepared Statements很像存储过程,是一种运行在后台的SQL语句集合,我们可以从使用 prepared statements 获得很多好 ...
- Go -- 判断chan channel是否关闭的方法
如果不判断chan是否关闭 Notice: 以下代码会产生死循环 代码如下: package main import ( "fmt" ) func main() { c := ma ...
- Zabbix监控Mongo
安装Zabbix-agent # groupadd zabbix # useradd -g zabbix zabbix # yum -y install gcc mysql-community-dev ...
- 图片异步载入之 Android-Universal-Image-Loader
今天在做项目的时候用了之前写的图片载入类.尽管也能实现缓存什么的.可是在载入大图的时候非常慢非常慢.于是上网找解决方式,准备优化一下,无意中发现了Android-Universal-Image-Loa ...