\(next\)应用,将原串视作最长前缀复制后的子串

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; int nxt[1000007];
char str[1000007];
int main(){
int len;
io >> len;
scanf("%s", str);
long long ans = 0;
int i = 0, j = -1;
nxt[0] = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
nxt[++i] = ++j;
while(nxt[nxt[i]] > 0) nxt[i] = nxt[nxt[i]];
if(nxt[i] > 0){
ans += i -nxt[i];
}
}
else
j = nxt[j];
} printf("%lld", ans);
return 0;
}

最新文章

  1. 基于mysql的数据管理
  2. MAC使用CocoaPods
  3. Android -- android:configChanges
  4. MZhong&#39;s Cover Letter
  5. log4j中文乱码解决方案
  6. 7-zip的压缩的时候排除某目录
  7. opacity兼容写法
  8. 手把手教你玩转Git分布式版本控制系统! (转载)
  9. DirFile
  10. page61-将二分查找重写为一段面向对象的程序
  11. java web接收POST数据
  12. Map.EntrySet的使用方法
  13. (Android) Chinese Character
  14. web前段学习2016.6.6
  15. windows service承载的web api宿主搭建(Microsoft.Owin+service)
  16. 当mysql报错1045时的解决方法
  17. #191 sea(动态规划)
  18. windows下安装Erlang
  19. ASP.NET Web API 框架研究 Controller创建过程与消息处理管道
  20. 有向图与无向图的合并操作区别D(递归与并查集)

热门文章

  1. 【单片机】使用 sscanf 提取AT命令返回结果中的有效数据
  2. 详解TCP四次挥手(断开TCP连接过程)
  3. R数据分析:如何简洁高效地展示统计结果
  4. Java - 六原则一法则
  5. dotnet core 也能协调分布式事务啦!
  6. Linux服务器启动jstatd服务
  7. 开发工具-RSA加解密
  8. Redis 切片集群的数据倾斜分析
  9. Vue回炉重造之三次封装axios
  10. SAP SD-Invoice 销售发票