题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629

题意求字符串的每个后缀与原串的最长公共前缀之和。

比赛时搞东搞西的,还搞了个后缀数组...队友一说扩展kmp我都自闭了,这不就是扩展kmp的第一步,求原串的每个后缀与原串的最长公共前缀嘛。

需要注意的就是题目准确问的是按照文中所给的代码执行需要判断几次,如果最长公共前缀等于该后缀的长度,则会判断Next[i]次(Next[i]为以i为开始的后缀与原串的最长公共前缀)。如果不等,则会判断Next[i]+1次,因为会判断一次失配。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
int Next[maxn];
void getN(char *s1) {//求子串与自身匹配
int i = , j, p, len = strlen(s1);
Next[] = len;
while (i + < len&&s1[i] == s1[i + ])
i++;
Next[] = i;
p = ;
for (i = ; i < len; i++) {
if (Next[i - p] + i < Next[p] + p)
Next[i] = Next[i - p];
else {
j = Next[p] + p - i;
if (j < )
j = ;
while (i + j < len&&s1[j] == s1[i + j])
j++;
Next[i] = j;
p = i;
}
}
}
char s[maxn];
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%s", s);
int n = strlen(s);
getN(s);
long long ans = ;
for (int i = ; i < n; ++i) {
if (Next[i] == n - i) ans += n - i;
else ans += Next[i] + ;
}
printf("%lld\n", ans); }
}

最新文章

  1. [JSP]自定义标签库taglib
  2. Linux环境下Android开发环境的搭建
  3. Codeforces 740A. Alyona and copybooks 模拟
  4. I&rsquo;ve seen the world,lit it up as my stage now
  5. SQL高级查询技巧(两次JOIN同一个表,自包含JOIN,不等JOIN)
  6. JS写的CRC16校验算法
  7. get与post
  8. JDK和Tomcat的安装与配置
  9. MVC5框架解析之MvcHandler
  10. 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy
  11. MySQL获取系统性能和状态
  12. Git 系列(四):在 Git 中进行版本回退
  13. java/php/c#版rsa签名以及验签实现
  14. OSX: 使用命令行对FileVault2分区恢复
  15. python学习笔记之socket(第七天)
  16. POJ 1845 Sumdiv(逆元)
  17. React Native使用init新建项目出现异常
  18. git 中merge的用法
  19. SQL 语句 explain 分析
  20. volatile的实现原理与应用

热门文章

  1. Spring Boot整合tk.mybatis及pageHelper分页插件及mybatis逆向工程
  2. 【NOIP2013模拟】DY引擎
  3. ios input readonly失效(点击的时候会有光标出现)/禁止输入法弹出问题
  4. BZOJ 3329: Xorequ(数位dp+递推)
  5. docker 在centos上的安装实践
  6. 大型网站技术架构,4网站的高性能架构之Web前端性能优化
  7. seq使用
  8. Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.
  9. H5 刮图-刮一刮
  10. ArrayDeque 源码分析