博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【HDU4622】Reincarnation
阅读量:4620 次
发布时间:2019-06-09

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

一眼似乎不可做,但发现\(strlen(x)\)很小,暴力\(O(n^2)\)预处理每个区间\((l,r)\),查询时\(O(1)\)输出就好了

#include
#include
#include
#include
#include
typedef int LL;const LL maxn=2010;inline LL Read(){ LL x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar(); return x*f;}struct node{ LL len,fail; LL son[26];}t[maxn<<1];LL n,m,T,nod,last;LL ans[maxn][maxn];char s[maxn];inline void Insert(LL c){ LL np=++nod,p=last; last=np; t[np].len=t[p].len+1; while(p&&!t[p].son[c]){ t[p].son[c]=np, p=t[p].fail; } if(!p) t[np].fail=1; else{ LL q=t[p].son[c]; if(t[q].len==t[p].len+1) t[np].fail=q; else{ LL nq=++nod; t[nq]=t[q]; t[nq].len=t[p].len+1; t[np].fail=t[q].fail=nq; while(p&&t[p].son[c]==q){ t[p].son[c]=nq, p=t[p].fail; } } }}int main(){ T=Read(); while(T--){ scanf(" %s",s+1); LL Len=strlen(s+1); for(LL i=1;i<=Len;++i){ nod=last=1; memset(t,0,sizeof(t)); for(LL j=i;j<=Len;++j){ Insert(s[j]-'a'), ans[i][j]=ans[i][j-1]+t[last].len-t[t[last].fail].len; } } m=Read(); while(m--){ LL l=Read(),r=Read(); printf("%d\n",ans[l][r]); } } return 0;}

转载于:https://www.cnblogs.com/y2823774827y/p/10201485.html

你可能感兴趣的文章
connection string for Excel/Access 2010
查看>>
【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
查看>>
学习wavenet_vocoder之环境配置
查看>>
常用Maven命令
查看>>
Docker启动mysql的坑2
查看>>
j2ee爬坑行之二 servlet
查看>>
JAVA基础入门(JDK、eclipse下载安装)
查看>>
最基础的applet运用--在applet上画线
查看>>
并不对劲的hdu4777
查看>>
linux使用rz、sz快速上传、下载文件
查看>>
判断数字的正则表达式
查看>>
DOC常用命令(转)
查看>>
php写一个判断是否有cookie的脚本
查看>>
Mac配置Fiddler抓包工具
查看>>
转:Java并发集合
查看>>
Word截图PNG,并压缩图片大小
查看>>
Python项目对接CAS方案
查看>>
mysql产生随机数
查看>>
编程风格
查看>>
熟悉常用的Linux命令
查看>>