Description

Given a string, find a substring of it which the original string contains exactly n such substrings.

Input

There are several cases. The first line of each case contains an integer n.The second line contains a string, no longer than 100000.

Output

If the such substring doesn't exist, output "impossible", else output the substring that appeared n times in the original string.If there are multiple solutions, output lexicographic smallest substring.

Sample Input

2
ababba

Sample Output

ab

题目大意:给一个字符串,找出其恰好出现n次的字典序最小的子串。
题目分析:将所有的子串排序后,定义n块为相邻的n个子串构成的字符串集合,如果某个n块的lca大于包含它的n+1块的lca,那么这个n块的lca便是恰好出现了n次的子串。 代码如下:
//# define AC

# ifndef AC

# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<cmath>
# include<set>
# include<map>
# include<string>
# include<cstdlib>
# include<algorithm>
using namespace std; const int N=100000; int SA[N+5];
int tSA[N+5];
int rk[N+5];
int cnt[N+5];
int height[N+5];
int *x,*y; bool same(int i,int j,int k,int n)
{
if(y[i]!=y[j]) return false;
if(i+k<n&&j+k>=n) return false;
if(i+k>=n&&j+k<n) return false;
return y[i+k]==y[j+k];
} void buildSA(char* s)
{
x=rk,y=tSA;
int m=130;
int n=strlen(s);
for(int i=0;i<m;++i) cnt[i]=0;
for(int i=0;i<n;++i) ++cnt[x[i]=s[i]];
for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;--i) SA[--cnt[x[i]]]=i; for(int k=1;k<=n;k<<=1){
int p=0;
for(int i=n-k;i<n;++i) y[p++]=i;
for(int i=0;i<n;++i) if(SA[i]>=k) y[p++]=SA[i]-k; for(int i=0;i<m;++i) cnt[i]=0;
for(int i=0;i<n;++i) ++cnt[x[y[i]]];
for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;--i) SA[--cnt[x[y[i]]]]=y[i]; p=1;
swap(x,y);
x[SA[0]]=0;
for(int i=1;i<n;++i)
x[SA[i]]=same(SA[i],SA[i-1],k,n)?p-1:p++;
if(p>=n) break;
m=p;
}
} void getHeight(char* s)
{
int n=strlen(s);
int k=0;
for(int i=0;i<n;++i) rk[SA[i]]=i;
for(int i=0;i<n;++i){
if(rk[i]==0)
height[rk[i]]=k=0;
else{
if(k) --k;
int j=SA[rk[i]-1];
while(s[i+k]==s[j+k]) ++k;
height[rk[i]]=k;
}
}
} char s[N+5];
int st[N+5][20]; void spare_table()
{
int n=strlen(s);
for(int i=1;i<n;++i)
st[i][0]=height[i];
for(int k=1;(1<<k)<=n;++k){
for(int i=1;i+(1<<k)-1<n;++i){
st[i][k]=min(st[i][k-1],st[i+(1<<(k-1))][k-1]);
}
}
} int getST(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1) ++k;
return min(st[l][k],st[r-(1<<k)+1][k]);
} string solve(int m)
{
string res="";
int n=strlen(s);
int a,b,c;
for(int i=0;i+m-1<n;++i){
a=b=c=0;
if(m==1) a=n-SA[i];
else a=getST(i+1,i+m-1);
if(i+m<n) b=getST(i+1,i+m);
if(i>0) c=getST(i,i+m-1);
if(a>b&&a>c){
for(int j=SA[i];j<SA[i]+a;++j) res+=s[j];
return res;
}
}
return "impossible";
} int main()
{
int m;
while(~scanf("%d",&m))
{
scanf("%s",s);
buildSA(s);
getHeight(s);
spare_table();
cout<<solve(m)<<endl;
}
return 0;
} # endif

  

FZU-2075 Substring(后缀数组)的更多相关文章

  1. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  2. HDU 5769 Substring 后缀数组

    Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...

  3. HDU 5679 Substring 后缀数组判重

    题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...

  4. hdu_1403_Longest Common Substring(后缀数组的应用)

    题目链接:hdu_1403_Longest Common Substring 题意: 给你两个字符串,然你找最长的公共子串 题解: 后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的 ...

  5. POJ3693 Maximum repetition substring 后缀数组

    POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...

  6. 2016多校联合训练4 F - Substring 后缀数组

    Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...

  7. hdu 5769 Substring 后缀数组 + KMP

    http://acm.hdu.edu.cn/showproblem.php?pid=5769 题意:在S串中找出X串出现的不同子串的数目? 其中1 <= |S| < $10^5$ 官方题解 ...

  8. Substring (后缀数组 + 计数)

    题意:求出字符串中包含了某个字符的字符序列不一样的数量. 思路:其实主要的是找出每个被包含字符的数量,假设除了目标字符之外的所有字符都不一样,那么应该就很好求了,但是显然不可能,所以我们可以枚举每一个 ...

  9. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

  10. POJ 3693 Maximum repetition substring ——后缀数组

    重复次数最多的字串,我们可以枚举循环节的长度. 然后正反两次LCP,然后发现如果长度%L有剩余的情况时,答案是在一个区间内的. 所以需要找到区间内最小的rk值. 两个后缀数组,四个ST表,$\Thet ...

随机推荐

  1. android 有弹性的ScrollView 简单实现,与处理ScrollView和ListView,GridView之间的冲突

    处理ScrollView和ListView,GridView之间的冲突, 最好的办法就是继承这两个类,重写他们的onMeasure方法即可: ListView: import android.widg ...

  2. gucci fake bags is usually really a sign of luxurious

    As soon as the violent trembling from the planet, standing company, people will certainly need to st ...

  3. AngularJs的UI组件ui-Bootstrap分享(九)——Alert

    alert指令会在页面上显示一条提示消息,效果是这样: 代码为: <!DOCTYPE html> <html ng-app="ui.bootstrap.demo" ...

  4. jquery checkbox实例

    <!DOCTYPE html><html> <head><meta charset="utf-8" /><title>& ...

  5. tesseract 编译与使用(windows)

    tesseract是google的一个开源OCR项目,项目地址已经迁移到github(现在 2016/09),地址 https://github.com/tesseract-ocr/tesseract ...

  6. if 判断中出现逗号

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. thymeleaf常用标签

    1. th:checked ,th:selected标签<input type="radio" value="M" name="gender&q ...

  8. excel表数据对比 个人收集

    做了那么久猿,转行做测试以后居然折堕到要用excel来对比数据...~—~.真是人算不如天算...不过没关系,技多不压身. 首先,准备好两个对比的数据表,sheet1 跟sheet2 .在sheet1 ...

  9. sql server数据库连接问题处理

    下面请一字一句地看,一遍就设置成功,比你设置几十遍失败,费时会少得多. 首先,在连接数据库之前必须保证SQL Server 2012是采用SQL Server身份验证方式而不是windows身份验证方 ...

  10. Spark相关错误汇总

    前面介绍了Spark开发环境的搭建,下面将在实际开发过程中遇到的一些问题汇总一下: 1.Exception in thread "main" com.typesafe.config ...