题目描述:

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

  1. Input:
  2. 1
    lmaf
    3
    0 1
    1 2
    2 3
  3.  
  4. Output:
  5. aflm
  6.  
  7. 题目的意思是给你一个字符串,然后给你n对位置,每一对位置上的两个字符可以进行无数次交换,问你进行足够多的交换后,你能得到的最小字典序的字符串是什么?
  8.  
  9. 如果想到并查集的话,就是把这n对位置进行集合上的合并,然后就变成了几个集合,这几个集合里面存的是一堆位置,在同一集合的位置能互换。
    现在我们只要每个集合中位置所对应的字符按照字典序排个序,每个集合都满足字典序最小,这样整体拼起来就也应该是最小的字典序。
  10.  
  11. 代码如下:
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int maxn =;
  5. vector<char> e[maxn];
  6. char s[maxn];
  7. int h[maxn];
  8. int father[maxn],l;
  9. int t,n;
  10. int Find(int x)
  11. {
  12. if (father[x]==x)
  13. return x;
  14. return father[x]=Find(father[x]);
  15. }
  16. void Union (int x,int y)
  17. {
  18. int fx=Find(x),fy=Find(y);
  19. if (fx!=fy);
  20. father[fx]=fy;
  21. }
  22. void init()
  23. {
  24. for (int i=;i<maxn;++i)
  25. e[i].clear();
  26. memset(h,,sizeof h);
  27. for (int i=;i<maxn;++i)
  28. father[i]=i;
  29. }
  30. int main()
  31. {
  32. //freopen("de.txt","r",stdin);
  33. scanf("%d",&t);
  34. while (t--){
  35. init();
  36. scanf("%s",s+);
  37. l=strlen(s+);
  38. scanf("%d",&n);
  39. for (int i=;i<n;++i){
  40. int a,b;
  41. scanf("%d%d",&a,&b);
  42. Union(++a,++b);
  43. }
  44. for (int i=;i<=l;++i)
  45. e[Find(i)].push_back(s[i]);//将对应位置的字符加入vector,以便排序
  46. for (int i=;i<=l;++i)
  47. sort(e[i].begin(),e[i].end());//将每个集合中的字母排序
  48. for (int i=;i<=l;++i){
  49. int tmp=Find(i);//找到当前位置所在集合的代表元素
  50. printf("%c",e[tmp][h[tmp]++]);//输出当前集合的h[tmp]++个元素,h[]数组用于统计集合内已经输出了几个元素
  51. }
  52. printf("\n");
  53. }
  54. return ;
  55. }
  1.  

SPOJ LEXSTR 并查集的更多相关文章

  1. SPOJ IAPCR2F 【并查集】

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

  2. SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集

    [题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...

  3. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  4. SPOJ:Lost and survived(multiset+并查集)

    On September 22, 2004, Oceanic Flight 815 crashed on a mysterious island somewhere in the pacific. T ...

  5. Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)

    题目链接:http://www.spoj.com/problems/IAPCR2F/en/ 题目大意: 给m个数字代表的大小,之后n组数据,两两关联,关联后的所有数字为一组,从小到大输出组数以及对应的 ...

  6. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  7. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  8. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  9. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

随机推荐

  1. [20190727NOIP模拟测试9]单(single) 题解(树上dp)

    啊啊啊啊啊啊啊啊考场上差一点就A掉了5555 千里之堤溃于蚁穴……鬼知道最后一步那么显然的柿子我为什么没考虑用上…… 观察数据范围可知,出题人期望我们想出一个$O(n)$的做法 当然也有可能是$O(n ...

  2. Transform.Find()

    代码演示: using System.Collections;using System.Collections.Generic;using UnityEngine; public class Tran ...

  3. LINUX shell脚本相关

    调试脚本 测试脚本语法:bash -n file.sh 查看脚本每一步执行情况:bash -x file.sh   位置变量:$1,$2,... 特殊变量:           %?:最后一个命令的执 ...

  4. JVM调优(四)——tomcat远程debug

    JVM调优(四)--tomcat远程debug tomcat远程debug jdwp协议 使用步骤 登录远程服务器,进入tomcat目录,并打开文件: //tomcat/bin/startup.sh ...

  5. ceph-cluster map

    知道cluster topology,是因为这5种cluster map. ====================================== 知道cluster topology,是因为这 ...

  6. Python笔记(十一)_匿名函数与map()、filter()

    匿名函数 无需显式定义函数名,和函数过程,使代码更精简的lambda表达式 函数没有命名,不用担心函数名的冲突 冒号前面代表函数的参数,后面表示计算过程 >>>func=lambda ...

  7. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalabl ...

  8. JAVA中的面向对象与内存解析_2

    构造方法(构造函数)   • 使用new +构造方法创建一个新的对象. • 构造函数是定义在Java类中的一个用来初始化对象的函数. • 构造函数与类同名且没有返回值. • 例如:Person类的构造 ...

  9. python 自动把mysql备份文件发送邮箱

    import os import time import sched import smtplib from email.mime.text import MIMEText from email.he ...

  10. 深入理解__proto__ 、constructor和prototype的关系

    深入理解__proto__ .constructor和prototype的关系 2013-11-12 09:56 1390人阅读 评论(3) 收藏 举报  分类: 前端之Javascript(59)  ...