van had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, …, xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
总结一下就是给你一个字符串的若干部分和他们的出现的位置,让你去输出这个字符串且按照最小字典序输出。
思路:首先应该明白并查集数组里面到底储存的是什么?储存的是跟答案密切相关的因素(废话)换句话说就是记录了基于当前题意下,元素与元素之间的关系(比如poj1182食物链中,pre数组就存储的是动物之间同类或者捕食,而这正是题意中所给出的元素与元素之间的关系,还有hdu3038判断集合错误个数中,pre数组就存储的是每个节点的父亲节点,这是判断是否冲突时的判断条件。而在这题中,pre数组则存储的是当前位置是否被占用,如果占用则后继到哪个位置没被占用,本题正是通过这一点来实现字符的赋值和优化)
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int pre[maxn<<1];
char str[maxn],ch[maxn<<1];//扩大二倍是为了防止超出maxn,因为你是在maxn后又加了一个str
int len;
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx<fy)
pre[fx]=fy;
else
pre[fy]=fx;
}
void solve(int k)
{
for(int i=k;i<len+k;i++)
{
i=find(i);
if(i<len+k)
{
ch[i]=str[i-k];
join(i,i+1);
}
}
}
void ini()
{
memset(ch,0,sizeof(ch));
for(int i=0;i<maxn<<1;i++)
pre[i]=i;
}
int main()
{
int n,m,k,mx=0;
scanf("%d",&n);
ini();
for(int i=0;i<n;i++)
{
getchar();
scanf("%s %d",str,&m);
len=strlen(str);
for(int j=1;j<=m;j++)
{
scanf("%d",&k);
mx=max(mx,k+len);
solve(k);
}
}
for(int i=1;i<mx;i++)
{
if(!ch[i])
printf("a");
else
printf("%c",ch[i]);
}
puts("");
return 0;
}
最后还是说一下,思路源自sdp学长,人帅性格好。