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 ...
随机推荐
- 2017 ACM/ICPC Asia Regional Guangxi Online 记录
题目链接 Guangxi 感觉这场比赛完全是读题场啊…… 比赛过程中丢失了一波进度,最后想开题的时候已经来不及了…… Problem A 按题意模拟……按照那个矩阵算就可以了 #include &l ...
- go语言学习之路四:字典
关联数组:(哈希或者字典) Map是go内置关联数据类型,字典是通过Key来访问Value的,访问格式如下: Value=mapName[key] 其实数组可以看做是一个键值类型为整型的字典,可以说数 ...
- Object中的wait,notify,notifyAll基本使用(转)
让线程停止运行/睡眠的方法只有两个:Thread.sleep()或者obj.wait() 记住obj.nofity()并不能停止线程运行,因为notify虽然释放了锁,但依然会急促执行完synchro ...
- METEOR_PACKAGE_DIRS 无效
windows中设置METEOR_PACKAGE_DIRS不起作用,一直提示找不到PACKAGES的原因. METEOR_PACKAGE_DIRS设置的路径太长了. 在系统属性 -->高级--& ...
- 梯度下降和EM算法,kmeans的em推导
I. 牛顿迭代法给定一个复杂的非线性函数f(x),希望求它的最小值,我们一般可以这样做,假定它足够光滑,那么它的最小值也就是它的极小值点,满足f′(x0)=0,然后可以转化为求方程f′(x)=0的根了 ...
- es删除文档或者删除索引
es删除文档或者删除索引 学习了:https://www.imooc.com/video/15771 删除文档: DELETE http://127.0.0.1:9200/people/man/1 删 ...
- Bootstrap全局CSS样式之button和图片
.btn-default--button的默认样式. .btn-primary--button的首选样式: .btn-success--button的成功样式: .btn-info--button的一 ...
- 在Nginx上部署ThinkPHP,解决Pathinfo问题
在Nginx上部署ThinkPHP,解决Pathinfo问题 事实上.要解决nginx不支持pathinfo的问题.有两个解决思路,一是不使用pathinfo模式,二是改动nginx的配置文件,使它支 ...
- 删除DataGridView选中行并更新数据库
前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会常常出现,也非常有用.通过DataGridView对数据库进行更改和查询都比較方便. 这里我们须要 ...
- UP board 漫谈——从Atom到UP Board
前言 原创文章,转载引用务必注明链接.如有疏漏,欢迎指正. 图文部分引用自CNXSoft 每块开发板都有其设计理念,也是其特色所在.有做工优良.接口丰富的多媒体全能开发板Lemaker Guita ...