Codeforces 802I Fake News (hard)
Codeforces 802I
题意:统计所有不同子串出现次数的平方的和。
想法:建一个SAM,$Ans=\sum (step[i]-step[fa[i]])*right[i]^2$
#include<cstdio>
#include<cstring>
#include<algorithm> typedef long long ll;
template
inline void read(T&x)
{
x=0;bool f=0;char c=getchar();
while((c<'0'||c>'9')&&c!='-')c=getchar(); if(c=='-')f=1,c=getchar();
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
x=f?-x:x;
}
const int MAXN(100010);
struct SAM{int nx[26],step,pre,right;}sam[MAXN<<1];int lastson,last,now=1,top=1,root=1;
int T,len;ll Ans;
char str[MAXN];
void extend(int x)
{
last=now; now=++top; sam[now].step=sam[last].step+1; sam[now].right=1;
for(;!sam[last].nx[x]&&last;last=sam[last].pre)
sam[last].nx[x]=now;
if(!last)sam[now].pre=root;
else
{
lastson=sam[last].nx[x];
if(sam[lastson].step==sam[last].step+1)sam[now].pre=lastson;
else
{
sam[++top]=sam[lastson];sam[top].right=0;
sam[top].step=sam[last].step+1;
sam[lastson].pre=sam[now].pre=top;
for(;sam[last].nx[x]==lastson&&last;last=sam[last].pre)
sam[last].nx[x]=top;
}
}
}
int cnt[MAXN],p[MAXN<<1];
void Total()
{
for(int i=1;i<=len;i++)cnt[i]=0;
for(int i=1;i<=top;i++)cnt[sam[i].step]++;
for(int i=1;i<=len;i++)cnt[i]+=cnt[i-1];
for(int i=top;i>=1;i--)p[cnt[sam[i].step]--]=i;
for(int i=top;i>=1;i--)sam[sam[p[i]].pre].right+=sam[p[i]].right;
for(int i=1;i<=top;i++)
{
Ans+=(ll)sam[i].right*sam[i].right*(sam[i].step-sam[sam[i].pre].step);
// fprintf(stderr,"%lld %d\n",(ll)sam[i].right*sam[i].right,sam[i].step);
}
}
int main()
{
// freopen("C.in","r",stdin);
read(T);
while(T--)
{
memset(sam,0,sizeof(sam));top=root=now=1;Ans=0;
scanf("%s",str+1);len=strlen(str+1);
for(int i=1;i<=len;i++)extend(str[i]-'a');
Total();
printf("%lld\n",Ans);
}
return 0;
}
Codeforces 802I Fake News (hard)的更多相关文章
- Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...
- [codeforces 804F. Fake bullions]
题目大意: 传送门. 给一个n个点的有向完全图(即任意两点有且仅有一条有向边). 每一个点上有$S_i$个人,开始时其中有些人有真金块,有些人没有金块.当时刻$i$时,若$u$到$v$有边,若$u$中 ...
- CodeForces 805A Fake NP
直觉. 一段区间中,肯定是$2$的倍数最多,因为区间长度除以$2$得到的数字最大.但只有$1$个数字的时候需要特判. #include <cstdio> #include <cmat ...
- codeforces411div.2
每日CF: 411div2 Solved A CodeForces 805A Fake NP Solved B CodeForces 805B 3-palindrome Solved C CodeFo ...
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题
B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- CodeForces - 556B Case of Fake Numbers
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
随机推荐
- @Html.AntiForgeryToken() 源码分析,表单防伪码的生成
源码来自MVC4@Html.AntiForgeryToken() 源码分析 public MvcHtmlString AntiForgeryToken() { return new MvcHtmlSt ...
- Note: Secure Deduplication with Efficient and Reliable Convergent Key Management (Dekey)
原文献:Secure Deduplication with Efficient and Reliable Convergent Key Management Dekey包含三个实体:用户(Users) ...
- Delphi Runtime Library在哪里?
Delphi Runtime Library是Delphi的运行时库,里面包含了大部分Delphi库的代码,这些库代码在哪里呢?其实正常安装完Dephi之后,在它的安装目录下面!下面我以我的Delph ...
- elementary os变成mac风(笔记)
sudo add-apt-repository ppa:philip.scott/elementary-tweaks && sudo apt-get update sudo apt-g ...
- Clean Code之JavaScript代码示例
译者按: 简洁的代码可以避免写出过多的BUG. 原文: JavaScript Clean Code - Best Practices 译者: Fundebug 本文采用意译,版权归原作者所有 引文 作 ...
- eclipse导出带有图片、音效、其他二进制文件的jar文件的经历
先说下简单流程吧: 1.选中“项目”,右键->export->java的“jar file”->勾选“export generated clas files and resource ...
- Gradle用户使用指南
转载请事先沟通,未经允许,谢绝转载. 1. 新工具介绍(Introduction) 能够复用代码和资源能够构建几种不同版本参数的应用能够配置.扩展.自定义构建过程1.1 为什么选择Gradle(Why ...
- [APIO2019T1]奇怪装置
考古学家发现古代文明留下了一种奇怪的装置.该装置包含两个屏幕,分别显示两个整数x和y.经过研究,科学家对该装置得出了一个结论:该装置是一个特殊的时钟,它从过去的某个时间点开始测量经过的时刻数t,但该装 ...
- AT2160 へんなコンパス / Manhattan Compass
传送门 乍一看像是一个计算几何,然后想到了BFS,但是苦于无奈\(O(n^2)\)不会优化 然后以下参考zjq_shadow大佬的思路 显然发现曼哈顿距离很麻烦,除了暴力枚举貌似没什么很好的办法 考虑 ...
- 忘记mysql数据库密码时进行修改方法
长时间没有使用数据库了,或者把密码改完之后就忘了数据库密码,不能正常进入数据库,也无法修改密码,有一个简单的常用修改密码方式: 1.首先找到和打开mysql.exe和mysqld.exe所在的文件夹( ...