SPOJ LEXSTR 并查集
题目描述:
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 题目的意思是给你一个字符串,然后给你n对位置,每一对位置上的两个字符可以进行无数次交换,问你进行足够多的交换后,你能得到的最小字典序的字符串是什么? 如果想到并查集的话,就是把这n对位置进行集合上的合并,然后就变成了几个集合,这几个集合里面存的是一堆位置,在同一集合的位置能互换。
现在我们只要每个集合中位置所对应的字符按照字典序排个序,每个集合都满足字典序最小,这样整体拼起来就也应该是最小的字典序。 代码如下:
#include <bits/stdc++.h> using namespace std;
const int maxn =;
vector<char> e[maxn];
char s[maxn];
int h[maxn];
int father[maxn],l;
int t,n;
int Find(int x)
{
if (father[x]==x)
return x;
return father[x]=Find(father[x]);
}
void Union (int x,int y)
{
int fx=Find(x),fy=Find(y);
if (fx!=fy);
father[fx]=fy;
}
void init()
{
for (int i=;i<maxn;++i)
e[i].clear();
memset(h,,sizeof h);
for (int i=;i<maxn;++i)
father[i]=i;
}
int main()
{
//freopen("de.txt","r",stdin);
scanf("%d",&t);
while (t--){
init();
scanf("%s",s+);
l=strlen(s+);
scanf("%d",&n);
for (int i=;i<n;++i){
int a,b;
scanf("%d%d",&a,&b);
Union(++a,++b);
}
for (int i=;i<=l;++i)
e[Find(i)].push_back(s[i]);//将对应位置的字符加入vector,以便排序
for (int i=;i<=l;++i)
sort(e[i].begin(),e[i].end());//将每个集合中的字母排序
for (int i=;i<=l;++i){
int tmp=Find(i);//找到当前位置所在集合的代表元素
printf("%c",e[tmp][h[tmp]++]);//输出当前集合的h[tmp]++个元素,h[]数组用于统计集合内已经输出了几个元素
}
printf("\n");
}
return ;
}
SPOJ LEXSTR 并查集的更多相关文章
- SPOJ IAPCR2F 【并查集】
思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...
- SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
[题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
- SPOJ:Lost and survived(multiset+并查集)
On September 22, 2004, Oceanic Flight 815 crashed on a mysterious island somewhere in the pacific. T ...
- Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)
题目链接:http://www.spoj.com/problems/IAPCR2F/en/ 题目大意: 给m个数字代表的大小,之后n组数据,两两关联,关联后的所有数字为一组,从小到大输出组数以及对应的 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
随机推荐
- Alex and Number
Alex and Number 时间限制: 1 Sec 内存限制: 128 MB提交: 69 解决: 12[提交][状态] 题目描述 Alex love Number theory. Today ...
- python里的深浅拷贝
拷贝就是拷贝,何来深浅之说? Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果 其实这个是由于共享内存导致的结果 拷贝:原则上就是把数据分离出来 ...
- 富文本编辑器——百度UEditor插件安装教程
一.使用环境 Win7 Eclipse jettty9 chrome 二.下载百度UEditor插件 1.下载地址:http://ueditor.baidu.com/website/download. ...
- mysql8.0 新特性,对json类型的常用操作
mysql8 新特性-json数据类型操作 -- 根据key(可多个)获取value SELECT JSON_EXTRACT('{"id": 14, "name" ...
- oracle 远程访问
oracle 本机能连外部访问不了 新装的 oracle 数据库经常会出现本地计算机能连接,但是局域网内的其他计算机不能连接的问题,如果出现此问题可以参考此文来解决. 本文中用的数据库版本为 Orac ...
- 测开之路二十六:Flask基础之最小web程序
Flask中文文档:http://docs.jinkan.org/docs/flask/ 安装Flask库 选端口号的一种方法(避免和别人选的端口冲突,小于1024的时候重新选) 最小web程序 用1 ...
- VB - 循环
循环控制语句有for……next循环.for……each循环.do……while循环.do……until循环.while循环五种形式. 1. 在使用循环控制语句前,首先要对循环条件进行判断,如果循环次 ...
- vue-router(路由)详细教程
vue-router(路由)详细教程:https://blog.csdn.net/wulala_hei/article/details/80488727 vue路由组件传参-页面通信:https:// ...
- leetcode.排序.451根据字符出现频率排序-Java
1. 具体题目 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r ...
- 在Ubuntu custom kernel上裝perf by compile
Using perf, the Linux Performance Analysis tool on Ubuntu Karmic A lot has been going on with Linux ...