HDU
来源:互联网 发布:22种免费网络推广方式 编辑:程序博客网 时间:2024/06/05 14:43
题目:给定一个字符串str,求str中至少出现2次且不覆盖的子串的个数
思路:每插入一个字符str[i],就把str+i+1在当前的SAM中跑一遍,算出每个接待最大匹配长度
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f//0x3f3f3f3fconst int MAXN = 2050, SIZE = 26;struct SAM { int len[MAXN], link[MAXN], next[MAXN][SIZE]; int total, last; inline int newNode(int L) { len[++total] = L; link[total] = 0; for(int i = 0; i < SIZE; ++i) next[total][i] = 0; return total; } inline void Add(int c) { int i, p = last, cur = newNode(len[last] + 1); for(; p && !next[p][c]; p = link[p]) next[p][c] = cur; if(!p) link[cur] = 1;//令其指向初始状态 else { int q = next[p][c]; if(len[q] == len[p] + 1) link[cur] = q; else {//> int clone = newNode(len[p] + 1); for(i = 0; i < SIZE; ++i) next[clone][i] = next[q][i]; link[clone] = link[q]; link[q] = link[cur] = clone;for(; p && next[p][c] == q; p = link[p]) next[p][c] = clone; } } last = cur; } void Init () {//根节点是1 total = 0; last = newNode(0); }}sam;char str[MAXN];int num[MAXN],idx[MAXN],match[MAXN];void topsort(){ mm(num,0); for(int i=1;i<=sam.total;i++) num[sam.len[i]]++; for(int i=1;i<=sam.total;i++) num[i]+=num[i-1]; for(int i=1;i<=sam.total;i++) idx[num[sam.len[i]]--]=i;}void query(char *str){ int now=1,len=0; for(int i=0;str[i]!='\0';i++){ int c=str[i]-'a'; if(sam.next[now][c]){ len++; now=sam.next[now][c]; } else{ while(sam.next[now][c]==0&&now!=0) now=sam.link[now]; if(now==0){ now=1; len=0; } else{ len=sam.len[now]+1; now=sam.next[now][c]; } } match[now]=max(match[now],len); }}void solve(){ int ans=0; for(int i=sam.total;i>=1;i--){ int p=idx[i],np=sam.link[p]; if(match[p]) match[np]=sam.len[np]; } for(int i=1;i<=sam.total;i++){ ans+=max(0,match[i]-sam.len[sam.link[i]]); } printf("%d\n",ans);}int main(){ while(~scanf("%s",str)){ if(str[0]=='#') break; sam.Init(); mm(match,0); int L=strlen(str); for(int i=0;i<=L-2;i++){ sam.Add(str[i]-'a'); query(str+i+1); } topsort(); solve(); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 设计模式之外观模式
- Escape character is '^]'.
- facebook开源项目全景投影转换Transform360
- Going Deeper into Regression Analysis with Assumptions, Plots & Solutions
- TCP/IP的开肠破肚(4)
- HDU
- JQuery学习系列基础教程
- PDF.JS的基本使用
- 如何正确的在VBA/VB中引用C#/VB.NET开发的DLL
- C++对象模型之内存布局三(虚继承)
- android:layout_marginEnd隐藏的坑
- HQL详细用法
- mac安装pytest
- 没有网线接口的MacBook电脑怎么连接有线网络?USB外置网卡来帮忙