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)的更多相关文章

  1. Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...

  2. [codeforces 804F. Fake bullions]

    题目大意: 传送门. 给一个n个点的有向完全图(即任意两点有且仅有一条有向边). 每一个点上有$S_i$个人,开始时其中有些人有真金块,有些人没有金块.当时刻$i$时,若$u$到$v$有边,若$u$中 ...

  3. CodeForces 805A Fake NP

    直觉. 一段区间中,肯定是$2$的倍数最多,因为区间长度除以$2$得到的数字最大.但只有$1$个数字的时候需要特判. #include <cstdio> #include <cmat ...

  4. codeforces411div.2

    每日CF: 411div2 Solved A CodeForces 805A Fake NP Solved B CodeForces 805B 3-palindrome Solved C CodeFo ...

  5. 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 ...

  6. 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

    题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...

  7. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  9. CodeForces - 556B Case of Fake Numbers

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

随机推荐

  1. 引用静态资源的url添加版本号,解决版本发布后的浏览器缓存有关问题

    在日常的工作中,我们经常会遇到页面文件(html,jsp等)中引用的js,css,图片等被修改后,而浏览器依然缓存着老版本的文件,客户一时半会看不到修改后的效果,同时也给生产环境的版本发布带来了一些问 ...

  2. Gson应用:利用map和list来拼装Json消息

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  3. OpenStack基础知识-tox的详解介绍

    1.tox简介 tox是通用的虚拟环境管理和测试命令行工具.tox能够让我们在同一个Host上自定义出多套相互独立且隔离的python环境,每套虚拟环境中可能使用了不同的 Python 拦截器/环境变 ...

  4. MATLAB求解非齐次线性方程组

    例如方程组: 法1:左除法 >> A=[3 1 -1;1 2 4;-1 4 5];b=[3.6;2.1;-1.4]; >> x=A\b x = 1.4818 -0.4606 0 ...

  5. bzoj2229: [Zjoi2011]最小割(最小割树)

    传送门 这题是用最小割树做的(不明白最小割树是什么的可以去看看这一题->这里) 有了最小割树就很简单了……点数那么少……每次跑出一个最大流就暴力搞一遍就好了 //minamoto #includ ...

  6. MCP|LQD|Data-independent acquisition improves quantitative cross-linking mass spectrometry (DIA方法可提升交联质谱定量分析)

    文献名:Data-independent acquisition improves quantitative cross-linking mass spectrometry (DIA方法可提升定量交联 ...

  7. plpython 中文分词Windows下 PG数据库jieba分词

    windows 下安装版本匹配python-3.4.3.amd64.msipostgresql-10.1-2-windows-x64.exe create language plpython3u;se ...

  8. P1168 中位数(对顶堆)

    题意:维护一个序列,两种操作 1.插入一个数 2.输出中位数(若长度为偶数,输出中间两个较小的那个) 对顶堆 维护一个小根堆,一个大根堆,大根堆存1--mid,小根堆存mid+1---n 这样堆顶必有 ...

  9. 自定义标签遇到的问题unable to load tag handler class "XX" for tag "XX"

    xxxx.tld文件的<tag-class>路径不正确,当时把until写成util了 摘自:http://zhidao.baidu.com/link?url=HP4tjHit2EXokI ...

  10. sortable结合angularjs实现拖动排序

    记录拖动排序 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...