解题:NOI2018 你的名字(68pts暴力)
2024-09-04 15:08:58
rt,如果省选没退役就补
SAM的优势:简单明了
先建S的SAM并标记所有节点,之后每次询问直接把T按广义SAM的方法插上去,统计新加的节点到根的状态代表的本质不同子串数,减掉被标记的部分就是T独有的
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int trs[N][],fth[N],len[N],vis[N],ori[N];
int T,t1,t2,lth,tot,lst,cnt,tmp,tep;
char str[N]; vector<int> vec;
int Insert(int ch)
{
int nde=lst,newn=++tot;
lst=newn,len[newn]=len[nde]+;
while(nde&&!trs[nde][ch])
trs[nde][ch]=newn,nde=fth[nde];
if(!nde) fth[newn]=;
else
{
int tran=trs[nde][ch];
if(len[tran]==len[nde]+)
fth[newn]=tran;
else
{
int rnde=++tot;
len[rnde]=len[nde]+,ori[rnde]=ori[tran];
for(int i=;i<=;i++) trs[rnde][i]=trs[tran][i];
fth[rnde]=fth[tran],fth[tran]=fth[newn]=rnde;
while(nde&&trs[nde][ch]==tran)
trs[nde][ch]=rnde,nde=fth[nde];
}
}
return newn;
}
void Clean()
{
for(int i=;i<(int)vec.size();i++)
{
int nde=vec[i];
while(nde&&vis[nde])
vis[nde]=false,nde=fth[nde];
}
}
int main()
{
scanf("%s%d",str+,&T);
lst=tot=,lth=strlen(str+);
for(int i=;i<=lth;i++)
ori[Insert(str[i]-'a')]=true;
while(T--)
{
scanf("%s%d%d",str+,&t1,&t2);
vec.clear(),lst=,lth=strlen(str+);
for(int i=;i<=lth;i++)
vec.push_back(Insert(str[i]-'a'));
long long ans=,anss=;
for(int i=;i<(int)vec.size();i++)
{
int nde=vec[i];
while(nde&&!vis[nde])
{
ans+=len[nde]-len[fth[nde]];
if(ori[nde]) anss+=len[nde]-len[fth[nde]];
vis[nde]=true,nde=fth[nde];
}
}
printf("%lld\n",ans-anss),Clean();
}
return ;
}
最新文章
- Table
- gulp教程之gulp-minify-css
- webdriver 获取佰词斩的单词(涉及字符串转换)
- K-means算法及文本聚类实践
- 【leetcode❤python】 414. Third Maximum Number
- innertext与innerhtml
- Tengine新增健康检查模块
- JS实现点击按钮,自增输入框个数
- IOS支付宝支付出现6002问题的解决办法
- 《TCP/IP具体解释》读书笔记(18章)-TCP连接的建立与中止
- java动态代理(JDK和cglib实现对比)
- [物理学与PDEs]第2章第2节 粘性流体力学方程组 2.1 引言
- Disruptor 为什么这么快?
- MySQL中间件之ProxySQL(1):简介和安装
- PostgreSQL 自动输入密码
- 错误:update 忘了加 where
- PCL点云特征描述与提取(2)
- 使用 urllib 解析 URL 链接
- lua元表(metatable)和元方法(metamethod)
- 十二、jdk工具之jcmd介绍(堆转储、堆分析、获取系统信息、查看堆外内存)