SPOJ-New Distinct Substrings,注意会爆int
SUBST1 - New Distinct Substrings
和上一题题意一样,只是数据范围有所改动,50000。
思路还是和上一题一样,所有字串数(len+1)*len/2。注意这里可能爆int,所有需要处理一下,然后减去height数组。
char s[N];
int sa[N],Rank[N],height[N],c[N],t[N],t1[N],n,m;
void build(int n)
{
// printf("n=%d m=%d\n",n,m);
int i,*x=t,*y=t1;
memset(c,0,sizeof(c));
for(i=0; i<n; i++) c[x[i]=s[i]]++;
for(i=1; i<m; i++) c[i]+=c[i-1];
for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i;
for(int k=1; k<=n; k<<=1)
{
int p=0;
for(i=n-k; i<n; i++) y[p++]=i;
for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k; memset(c,0,sizeof(c));
for(i=0; i<n; i++) c[x[y[i]]]++;
for(i=1; i<m; i++) c[i]+=c[i-1];
for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1,x[sa[0]]=0;
for(i=1; i<n; i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
if(p>=n) break;
m=p;
}
}
void get_height(int n)//n不保存最后的0
{
int i,j,k=0;
for(i=1; i<=n; i++) Rank[sa[i]]=i;
for(i=0; i<n; i++)
{
if(k) k--;
j=sa[Rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[Rank[i]]=k;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
n=strlen(s);
s[n]=0,m=125;//m在build函数的基数排序时会改变,所以每次都需要赋初值。
build(n+1);
get_height(n);
int ans;
if(n%2) ans=(n+1)/2*n;
else ans=n/2*(n+1);
for(int i=1;i<=n;i++) ans-=height[i];
printf("%d\n",ans);
}
return 0;
}
心力交瘁。。。。。卒
SPOJ-New Distinct Substrings,注意会爆int的更多相关文章
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- SPOJ 694 Distinct Substrings
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
- SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ 705 Distinct Substrings(后缀数组)
[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每 ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
随机推荐
- GIMP 无法设置中文的问题解决
首先按照网上说的安装了language-pack-gnome-zh-hant 参考链接:http://www.ubuntu-tw.org/modules/newbb/viewtopic.php?top ...
- ubuntu中wine下安装QQ
原文:http://jingyan.baidu.com/article/359911f55da27057fe0306d8.html 可以把win改成最新版
- python第一模块数据类型
一·进制之间的转换 十进制转换为二进制:逆序取余法. 二进制转换为十进制:如1101 1*2^0 + 0*2^1 + 1*2^2 +1 十六进制转换为二进制:231 0010 0011 ...
- ubuntu kylin 13.10 无法安装ia32-libs解决方案
1.安装 Synaptic 2.sudo apt-get install synaptic 3.进入synaptic ,设置->软件库 4.点击 其他软件->添加 5.输入“deb ht ...
- UVA1660 Cable TV Network (无向图的点连通度)
题意:求一个无向图的点连通度. 把一个点拆成一个入点和一个出点,之间连一条容量为1的有向边,表示能被用一次.最大流求最小割即可. 一些细节的东西:1.源点固定,汇点要枚举一遍,因为最小割割断以后会形成 ...
- 从汇编看c++中临时对象的析构时机
http://www.cnblogs.com/chaoguo1234/archive/2013/05/12/3074425.html c++中,临时对象一旦不需要,就会调用析构函数,释放其占有的资源: ...
- end和sep的使用方法
end: 默认是换行'\n',表示以什么结尾,比如以, | \n 等 方法: 默认end = '\n' a b c 如果end = ' ' a b c sep: 默认是空格' ' 表示两个字符之间用什 ...
- MVC使用方法
1.mvc打开html代码 后台处理: ///<summary> ///恢复html中的特殊字符 ///</summary> ...
- OpenCV2:总结篇 core模块
一.cv::Mat 1.作用 cv::Mat表示图像类,用来操作图像和矩阵,它包含很多属性和方法 2.构造方法 cv::Mat image; //cv::Mat image() 无参数构造 ...
- vs编译obj给delphi用
Cl /O2 /c bjhash.cpp 记得cl x32 和cl x64的区别