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

题目:


string matching

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Problem Description

String matching is a common type of problem in computer science. One string matching problem is as following:

Given a string s[0…len−1], please calculate the length of the longest common prefix of s[i…len−1] and s[0…len−1] for each i>0.

I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:

We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.

Input

The first line contains an integer T, denoting the number of test cases.
Each test case contains one string in a line consisting of printable ASCII characters except space.

* 1≤T≤30

* string length ≤106 for every string

Output

For each test, print an integer in one line indicating the number of compare operations invoked if we run the algorithm in the statement against the input string.

Sample Input

3

_Happy_New_Year_

ywwyww

zjczzzjczjczzzjc

Sample Output

17

7

32

解题思路:


新算法get✅

扩展KMP裸题,不能用后缀数组,倍增算法nlogn超时,DC3算法常数过大,同样超时!!

扩展kmp讲解参见https://www.jianshu.com/p/107e47994d49

ac代码:


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000010; //字符串长度最大值
int nxt[maxn];
char s[maxn];
ll  solve(char str[])
{ll ans = 0;int i=0,j,po,len=strlen(str);nxt[0]=len; //初始化nxt[0]while(str[i]==str[i+1] && i+1<len) i++; nxt[1]=i; //计算nxt[1]ans += nxt[1];if(nxt[1] != len - 1) ans++;po=1; //初始化po的位置for(i=2;i<len;i++){if(nxt[i-po]+i < nxt[po]+po) //第一种情况,可以直接得到nxt[i]的值nxt[i]=nxt[i-po];else //第二种情况,要继续匹配才能得到nxt[i]的值{j = nxt[po]+po-i;if(j<0) j=0; //如果i>po+nxt[po],则要从头开始匹配while(i+j<len && str[j]==str[j+i]) j++; nxt[i]=j;po=i; //更新po的位置}ans += nxt[i];if(nxt[i] != len - i) ans++;}return ans;
}
int main()
{//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);int t;scanf("%d", &t);while(t--){scanf("%s",s);printf("%lld\n", solve(s));}return 0;
}

【2019杭电多校第五场1006=HDU6629】string matching(求后缀字符串和本串的最长公共前缀长度之和--扩展kmp)相关推荐

  1. 2019杭电多校 第七场 Kejin Player 6656(求期望值)

    2019杭电多校 第七场 Kejin Player 6656(求期望值) 题目 http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意 给你n,q.表示有n ...

  2. 2019 杭电 多校第3场 1006 Fansblog (HDU 6608)

    题目链接 题解: 用威尔逊定理变换,然后求逆元. 代码: #include <bits/stdc++.h> using namespace std; typedef long long l ...

  3. 2019杭电多校第9场1002 Rikka with Cake HDU6681

    2019杭电多校第9场1002 Rikka with Cake HDU6681 题意:给你若干个点按上下左右切割平面,最后问平面内有几块被切割开来. 解法1:红黑树+思维+贪心 A:根据欧拉定理可以得 ...

  4. 【2019.08.21】2019杭电多校第十场

    补题地址:http://acm.hdu.edu.cn/listproblem.php?vol=58 题号:6691-6701 1001: 1002: 1003:✅ 1004: 1005:✅ 1006: ...

  5. 2019 杭电多校第六场 题解

    比赛记录 注意随机数据 ,1-n排列这种,一般都有啥暴力重构之类的方法,期望重构次数很少之类的 1005也是这样,因为n^2但只有n个值有数,所以就可以n^2logn 题解 1001 Salty Fi ...

  6. 2019杭电多校第三场 6608 Fansblog(威尔逊定理+miller_rabin素性测试)

    Problem Description 传送门 Farmer John keeps a website called 'FansBlog' .Everyday , there are many peo ...

  7. hdu 6656 2019杭电多校第7场 期望题

    设f[i]为从i升级到i+1期望需要的金钱,由于每级都是能倒退或者升级到i+1,所以询问从l,r的期望金钱可以直接前缀和,那么推导每一级升级需要的期望钱也可以用前缀和推导 设sum[i]=f[1]+f ...

  8. 2019杭电多校第7场 K Kejin Player HDU 6656(数学推导)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题目大意:对于每一个等级,可以花ai元,有pi概率升级,如果升级失败就退到xi级,问从li级升到 ...

  9. 2019杭电多校第七场 HDU - 6656 Kejin Player 期望

    题目链接:https://vjudge.net/problem/HDU-6656 题解: 维护一个前缀sum[i] : 从1到 i 的期望 第 i 到达 i + 1是:ai + (1 - r[i] / ...

  10. 2019杭电多校第七场 HDU - 6656 Kejin Player——概率期望

    题意 总共有 $n$ 层楼,在第 $i$ 层花费 $a_i$ 的代价,有 $pi$ 的概率到 $i+1$ 层,否则到 $x_i$($x_i \leq 1$) 层.接下来有 $q$ 次询问,每次询问 $ ...

最新文章

  1. BIOS系统服务 —— 直接磁盘服务(int 0x13)
  2. 如何实现linux+windows双系统启动
  3. Linux下FTP服务器配置与管理
  4. 三菱fx3uplc恢复出厂设置_三菱fx3uplc解密过程与步骤分享
  5. leetcode74. 搜索二维矩阵(二分查找)
  6. 在Windows Server 2003上运行vSphere Client 4.0出现“clients.xml文件出错r
  7. 恢复云数据库MySQL的备份文件到自建数据库
  8. OpenCV 1.0在VC6下安装与配置(附测试程序)
  9. Hibernate【2】——封装工具、HibernateUtil类以及DAO层的BaseDAO类
  10. oracle元转换为亿元,元换算成亿元(亿元和圆的换算)
  11. 【读书笔记->统计学】11-01 总体和样本的估计-总体均值、样本均值、点估计量、总体方差、估计总体方差概念简介
  12. 思科路由器及交换机基本配置
  13. Python 使用乐动体育的 backoff 更优雅的实现轮询
  14. ThreadPoolExecutor(七)——总结补充
  15. mac book pro 安装网络准入后经常死机
  16. ECC椭圆曲线算法(1)阿贝尔群
  17. 小米5s升级android9,小米5S lineage16 安卓9.0 极致省电 纯净 完美root Xposed 经典版
  18. 【读后感】《清单革命》读书笔记
  19. 电脑外设(I/O)简介:键盘鼠标
  20. 初探 Ettercap: ARP投毒 DNS欺骗

热门文章

  1. 利润百倍的暴利行业?
  2. 相对地址转化为绝对地址
  3. 庆祝自己通过系分考试,分发资料
  4. C/C++ 输入字符串gets( )、scanf( )、getline( )以及单个字符getchar( )
  5. 二维数组和稀疏数组转化
  6. 机器人煮面机创始人_煮面机器人会是未来的「食神」吗?
  7. erp系统原理和实施第五版pdf_ERP系统实施费用!
  8. java ocx调用_Javascript调用OCX控件
  9. 我的世界java版和基岩版对比_我的世界 Java版 与 基岩版 有什么区别?
  10. 烤箱定时器不走怎么办_走夜路害怕怎么办?带上这6大辟邪文玩在也不害怕