http://codeforces.com/contest/828/problem/C

【题意】

【思路】

  • 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间,把确定的点的父亲节点设为下一个点,这样访问过的点的根节点都是没访问过的点。
  • 与上一题是同样的思路,用并查集路径压缩,
  • 要求字典序最小,可以最初给每个字符都赋值'a'
  • 判断字符串最长是多少,最后加'\0'

【Accepted】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int N=1e6+;
const int maxn=*N;
const int inf=0x3f3f3f3f;
int fa[maxn];
int n,m;
int a[maxn];
char str[maxn];
char ans[maxn];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} void init()
{
for(int i=;i<maxn;i++){
fa[i]=i;
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
memset(ans,'a',sizeof(ans));
int mmax=-inf;
for(int i=;i<n;i++)
{
scanf("%s%d",str,&m);
int len=strlen(str);
int v=-inf;
for(int k=;k<m;k++)
{
scanf("%d",&a[k]);
v=max(v,a[k]);
}
mmax=max(mmax,v+len);
for(int k=;k<m;k++)
{
int x=a[k];
int y=a[k]+len-;
while((x=find(x))<=y)
{
ans[x]=str[x-a[k]];
fa[x]=x+;
}
}
}
ans[mmax]='\0';
cout<<ans+<<endl; }
return ;
}

或者

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int N=1e6+;
const int maxn=*N;
const int inf=0x3f3f3f3f;
int fa[maxn];
int n,m;
int a[maxn];
char str[maxn];
char ans[maxn];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} void init()
{
for(int i=;i<maxn;i++){
fa[i]=i;
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
memset(ans,'a',sizeof(ans));
int mmax=-inf;
for(int i=;i<n;i++)
{
scanf("%s%d",str,&m);
int len=strlen(str);
for(int k=;k<m;k++)
{
scanf("%d",&a[k]);
}
mmax=max(mmax,a[m-]+len);
for(int k=;k<m;k++)
{
int x=a[k];
int y=a[k]+len-;
while((y=find(y))!=find(x-))
{
ans[y]=str[y-a[k]];
fa[y]=fa[y-];
}
}
}
ans[mmax]='\0';
cout<<ans+<<endl; }
return ;
}

从右往左或从左往右合并都一样。

一开始在数据8上T了好几回,原来并查集的find一开始没注意没有路径压缩..........

【数轴涂色+并查集路径压缩+加速】C. String Reconstruction的更多相关文章

  1. 并查集+路径压缩(poj1988)

    http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submiss ...

  2. hdu 1558 线段相交+并查集路径压缩

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network

    Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...

  4. HDOJ 3635 并查集- 路径压缩,带秩合并

    思路来源:http://blog.csdn.net/niushuai666/article/details/6990421 题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城 ...

  5. LA 并查集路径压缩

    题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...

  6. snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)

    1110: 传输网络 Time Limit: 3 Sec  Memory Limit: 512 MBSubmit: 43  Solved: 18[Submit][Status][Web Board] ...

  7. - > 并查集+路径压缩(详解)(第一节)

    先举一个友爱的例子解释一下并查集: 话说江湖上散落着各式各样的大侠,有上千个之多. 他们没有什么正当职业,整天背着剑在外面走来走去,碰到和自己不是一路人的,就免不了要打一架.但大侠们有一个优点就是讲义 ...

  8. PAT甲级1013题解——并查集+路径压缩

    题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...

  9. HDU 3635 并查集+路径压缩+记录每个点移动次数

    题意: 给定n个点 oper个操作 每个点有1个龙珠 下面2种操作: T u v 把u点所有龙珠搬到v Q u  问u点当前所在城市 u点所在城市有几个龙珠 u点被移动几次 思路: 并查集可以求出 u ...

随机推荐

  1. Wannafly挑战赛10:A题:小H和迷宫

    题目描述       小H陷入了一个迷宫中,迷宫里有一个可怕的怪兽,血量有N点,小H有三瓶魔法药水,分别可以使怪兽损失a%.b%.c%的血量(之后怪兽的血量会向下取整),小H想合理地运用这三瓶药水,使 ...

  2. CoreText的绘制流程-转

    来自:http://blog.sina.com.cn/s/blog_7c8dc2d50101lbb1.html 使用coreText进行文本绘制,需要在工程中添加CoreText.framework, ...

  3. Join方法,yield方法,线程的优先级

  4. hdu 6012 Lotus and Horticulture 打标记

    http://acm.hdu.edu.cn/showproblem.php?pid=6012 我们希望能够快速算出,对于每一个温度,都能够算出它在这n颗植物中,能得到多少价值. 那么,对于第i科植物, ...

  5. .net 发送邮件验证码

    using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Ma ...

  6. Laravel环境搭建

    在有了初步认知后,当然就要开始在自己的电脑上搭建Laravel的开发环境了. 系统环境需求 PHP 5.3.7或者更高版本,如果没有系统没有安装PHP环境的,请到下面地址下载:http://cn2.p ...

  7. IIS7.0中Process打开cmd程序出现问题

    本人在VS中用Process打开cmd程序,并传入参数,转换图片,运行成功! 但是放入IIS7.0中,Process打不开cmd程序,程序直接运行过去,无结果,无报错! 解决方案: 将IIS里面你程序 ...

  8. 一个完整的http请求分析

    Request URL:http://localhost:8080/test.jhtmlRequest Method:POSTStatus Code:200 OKRemote Address:[::1 ...

  9. redis-cli 工具--raw参数的作用

    最近阅读了以redis官网关于--raw参数的解释,其功能有两个: 1.按数据原有格式打印数据,不展示额外的类型信息 例如:使用命令发送方式(redis在使用时有命令发送方式和交互方式两种)创建一个k ...

  10. redisd的非持久化配置

    如何关闭redis持久化?我的需求是只把redis当作缓存来用,所以持久化到硬盘对我的需求来说没有意义. 修改redis配置文件,redis.conf 第115行左右. 1.注释掉原来的持久化规则 # ...