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(并查集&排序)的更多相关文章

  1. FZU 2059 MM (并查集+排序插入)

    Problem 2059 MM Accept: 109    Submit: 484Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem ...

  2. SPOJ IAPCR2F 【并查集】

    思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...

  3. SPOJ LEXSTR 并查集

    题目描述: Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using ...

  4. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  5. hdu 1811Rank of Tetris (并查集 + 拓扑排序)

    /* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...

  6. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  7. 并查集+拓扑排序 赛码 1009 Exploration

    题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...

  8. LA 4255 (拓扑排序 并查集) Guess

    设这个序列的前缀和为Si(0 <= i <= n),S0 = 0 每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下. 还要注意一下前缀和相等的情况,所以用一个并查集来查询. ...

  9. Rank of Tetris(hdu1811拓扑排序+并查集)

    题意:关于Rating的信息.这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rati ...

随机推荐

  1. ScrollPic.js——图片左右滚动插件

    自己在做项目的时候刚好遇到了图片做左右滚动的项目,发现ScrollPic.js这个插件还是很好用的,一般的浏览器都支持,也可以做成bootstrap样式: 首先需要下载ScrollPic.js这个插件 ...

  2. Java Socket应用

    Java Socket(套接字)通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求.

  3. BZOJ——1614: [Usaco2007 Jan]Telephone Lines架设电话线

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1930  Solved: 823[Submit][Status][Discuss] Description ...

  4. T3185 队列练习1 codevs

    http://codevs.cn/problem/3185/ 题目描述 Description 给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请输出最终的队头元素. 操作解释:1表示 ...

  5. Codeforces Round #310 (Div. 2)简洁题解

    A:原来是大水题,我还想去优化.. 结果是abs(num('0')-num('1')); num表示一个符号的个数; B:暴力模拟即可,每次判断是否能构造出答案. C:俄罗斯套娃,套套套,捉鸡的E文. ...

  6. Codeforces 837 E Vasya's Function

    Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) =  ...

  7. how to avoid over-fitting?(机器学习中防止过拟合的方法,重要)

    methods to avoid overfitting: Cross-Validation : Cross Validation in its simplest form is a one roun ...

  8. BUPT复试专题—统计节点个数(2013)

    题目描述 给出一棵有向树,一共有n个节点,如果一个节点的度(入度+出度)不小于它所有儿子以及它父亲的度(如果存在父亲或儿子),那么我们称这个节点为p节点,现在你的任务是统计p节点的个数. 如样例,第一 ...

  9. 常用linux系统监控命令

    一.内存监控 监控内存的使用状态是非常重要的,通过监控有助于了解内存的使用状态,比如内存占用是否正常,内存是否紧缺等等,监控内存最常使用的命令有free.vmstat.top等 1.1 free $ ...

  10. Intel MIC

    http://en.wikipedia.org/wiki/Intel_MIC Intel MIC From Wikipedia, the free encyclopedia     Intel Man ...