博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3518 后缀数组
阅读量:7069 次
发布时间:2019-06-28

本文共 3389 字,大约阅读时间需要 11 分钟。

Boring counting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2906    Accepted Submission(s): 1201

Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
 

 

Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
 

 

Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
 

 

Sample Input
aaaa ababcabb aaaaaa #
 

 

Sample Output
2 3 3

 

 

 

/*hdu 3518 后缀数组problem:给你一个字符串,问它有多少种出现两次且不重叠的子串solve:因为总长度为1000,所以考虑通过枚举子串的长度来计算每种长度有多少种子串。但是感觉不知道怎么区分相同长度的不同串 T_T后来发现最开始思路有问题,对height分组讨论的时候。每一组height在同一长度下只可能有一种子串所以通过 枚举+判断 就能得出结果hhh-2016-08-12 10:29:23*/#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define lson (i<<1)#define rson ((i<<1)|1)typedef long long ll;typedef unsigned int ul;const int INF = 0x3f3f3f3f;const int maxn = 200000+10;const int mod = 1e9+7;int t1[maxn],t2[maxn],c[maxn];bool cmp(int *r,int a,int b,int l){ return r[a]==r[b] &&r[l+a] == r[l+b];}void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m){ n++; int p,*x=t1,*y=t2; for(int i = 0; i < m; i++) c[i] = 0; for(int i = 0; i < n; i++) c[x[i] = str[i]]++; for(int i = 1; i < m; i++) c[i] += c[i-1]; for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i; for(int j = 1; j <= n; j <<= 1) { p = 0; for(int i = n-j; i < n; i++) y[p++] = i; for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j; for(int i = 0; i < m; i++) c[i] = 0; for(int i = 0; i < n; i++) c[x[y[i]]]++ ; for(int i = 1; i < m; i++) c[i] += c[i-1]; for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x,y); p = 1; x[sa[0]] = 0; for(int i = 1; i < n; i++) x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++; if(p >= n) break; m = p; } int k = 0; n--; for(int i = 0; i <= n; i++) Rank[sa[i]] = i; for(int i = 0; i < n; i++) { if(k) k--; int j = sa[Rank[i]-1]; while(str[i+k] == str[j+k]) k++; height[Rank[i]] = k; }}int Rank[maxn],height[maxn];int sa[maxn];char str[maxn];int r[maxn];int cal(int len,int n){ int cnt = 0; int minx = 100000,maxx = 0; for(int i = 2;i <= n;i++) { if(height[i] >= len) { minx = min(minx,sa[i]),minx = min(minx,sa[i-1]); maxx = max(maxx,sa[i]),maxx = max(maxx,sa[i-1]);// cout << "max:" <
<< " " <<"min:" << minx <
= len) cnt ++; minx = 100000;maxx = 0; } } if(maxx - minx >= len) cnt ++; // cout << len << " " <
<
< len;i++) r[i] = str[i] - 'a'+1; r[len] = 0; get_sa(r,sa,Rank,height,len,128); int ans = 0; for(int i = 1;i <= len/2+1;i++) ans += cal(i,len); printf("%d\n",ans); }}

  

转载于:https://www.cnblogs.com/Przz/p/5792182.html

你可能感兴趣的文章
委托与事件的练习
查看>>
Netty源码分析(二):服务端启动
查看>>
nodejs-7.2. CURD数据管理系统小栗子
查看>>
addMusic 和playMusic(AVAudioPlayer)
查看>>
压力测试的轻量级具体做法
查看>>
又一款博客园Android客户端低调推出
查看>>
自定义Property属性动画
查看>>
WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
查看>>
苹果应用的上线步骤
查看>>
初识scoket
查看>>
使用Windows8开发Metro风格应用七
查看>>
(第一天)包装对象、作用域、创建对象
查看>>
POJ 3104:Drying(二分)
查看>>
Stardew Valley(星露谷物语)Mod开发之路 1环境配置
查看>>
2012 借教室
查看>>
为什么是Spring Boot
查看>>
前端之本http协议
查看>>
python基础-协程
查看>>
JavaScript数据类型
查看>>
zoj 1004 Anagrams by Stack (dfs+stack)
查看>>