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. 图片裁剪上传插件——jquery.photoClip.js

    想要裁剪图片上传: 需要依赖的的插件为: [jquery.photoClip.js] 插件[iscroll-zoom.js] 插件[hammer.js] 插件 [lrz.all.bundle.js] ...

  2. LeetCode OJ--Reverse Linked List II

    http://oj.leetcode.com/problems/reverse-linked-list-ii/ 链表的操作 #include <iostream> using namesp ...

  3. cobbler api接口开发测试实例

    条件1:必须搭建好cobbler服务,并且可以通过web访问:http://cobbler_ip/cobbler_web 测试可以打开.然后再用以下命令测试. #!/opt/python3/bin/p ...

  4. PAT (Advanced Level) 1086. Tree Traversals Again (25)

    入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...

  5. 虚拟机centos 里tomcat的端口映射到主机 Windows里面

  6. 安装Django时解决的问题-mysql及访问(附pycharm激活)

    1.做些软链接和virtualenv的基本使用: ln -s /data/linkdood/im/vrv/python36/bin/python3.6 /usr/bin/python3 ln -s / ...

  7. Hadoop HDFS 常用命名

    HDFS命令基本格式:hadoop fs -cmd < args > ls 命令hadoop fs -ls / 列出hdfs文件系统根目录下的目录和文件 hadoop fs -ls -R ...

  8. htmlcxx取指定字段实例

    #include <string> #include <iostream> #include <sstream> #include <htmlcxx/html ...

  9. 关于button中设置文字不显示的问题

    这个因为使用的image加载方式是setimage而不是setbackgroundimage导致文字始终出不来.

  10. Dubbo和Spring集成Demo

    Zookeeper安装和启动 http://mirrors.hust.edu.cn/apache/zookeeper/下载,我的版本是 3.4.5. 解压到 D:\zookeeper-3.4.5 配置 ...