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

题意:t 组数据,每组给出一个字符串 S,现在有一个函数,每次会暴力的跑 S 的所有后缀与字符串 S 的最长公共前缀,问这个函数会运行多少次

思路:

实质是求 S 的每个后缀与 S 的最长公共前缀的比较次数之和

扩展 KMP 模版题,求出 extend 数组后比较累加即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;int Next[N];
int extend[N];
void getNext(char *str) {int len = strlen(str);Next[0] = len;int i = 0;while (str[i] == str[i + 1] && i + 1 < len)i++;Next[1] = i;int pos = 1;for (i = 2; i < len; i++) {if (Next[i - pos] + i < Next[pos] + pos)Next[i] = Next[i - pos];else {int j = Next[pos] + pos - i;if (j < 0)j = 0;while (i + j < len && str[j] == str[j + i])j++;Next[i] = j;pos = i;}}
}
void exKMP(char *s1, char *s2) {getNext(s2);int len1 = strlen(s1);int len2 = strlen(s2);int i = 0;while (s1[i] == s2[i] && i < len2 && i < len1)i++;extend[0] = i;int pos = 0;for (i = 1; i < len1; i++) {if (Next[i - pos] + i < extend[pos] + pos)extend[i] = Next[i - pos];else {int j = extend[pos] + pos - i;if (j < 0)j = 0;while (i + j < len1 && j < len2 && s1[j + i] == s2[j])j++;extend[i] = j;pos = i;}}
}char s[N];
int main(){int t;scanf("%d",&t);while(t--){memset(Next,0,sizeof(Next));memset(extend,0,sizeof(extend));scanf("%s",s);exKMP(s,s);LL res=0;int len=strlen(s);for(int i=1;i<len;i++){res+=extend[i]+1;if(extend[i]+i>=len)res--;}printf("%lld\n",res);}return 0;
}

string matching(HDU-6629)相关推荐

  1. ES6, Angular,React和ABAP中的String Template(字符串模板)

    String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...

  2. 二进制数与十进制数相互转换 string陷阱(赋值错误) 汉字分为两个字符 string123 string字符串逆转

    二进制数与十进制数相互转换: 二进制数(数组)转换成十进制数的函数: BtoO(char str[]) { int lg,sum=0,j=1; lg=strlen(str)-1; for(;lg> ...

  3. 有源汇有上下界最大流/最小流 配题(HDU 3157)

    因为是有源汇所以设源点为 s,汇点为 t. 有源汇有上下界最大流: 连接一条 t 指向 s 的边,容量为 INF. 通过上述步骤,现在图变成了无源汇网络. 引入超级源点 S,超级汇点 T. 连接一条 ...

  4. HDU2019多校第二场 1009(HDU 6599) I Love Palindrome String(回文树(自动机)+manacher)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 解题思路: 回文自动机求每个本质不同的子串出现的次数,同时记录每个节点i代表的回文串第一次出现的 ...

  5. 最大表示法--环形字符串最大字典序(HDU 5442)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 问题概述:n个字符围成一个环,请从这个环中找出字典序最大的长度为n的字符串,输出它的起始点和方向(0顺1 ...

  6. Codeforces Round #617 (Div. 3) String Coloring(E1.E2)

    (easy version): 题目链接:http://codeforces.com/contest/1296/problem/E1 题目一句话就是说,两种颜色不同的字符可以相互换位, 问,对这字符串 ...

  7. 【论文简述及翻译】ACVNet:Attention Concatenation Volume for Accurate and Efficient Stereo Matching(CVPR 2022)

    一.论文简述 1. 第一作者:Gangwei Xu,Junda Cheng 2. 发表年份:2022 3. 发表期刊:CVPR 4. 关键词:端到端训练.代价体.注意力机制.3D聚合.回归 5. 探索 ...

  8. 【论文简述及翻译】GA-Net: Guided Aggregation Net for End-to-end Stereo Matching(CVPR 2019)

    一.论文简述 1. 第一作者:Feihu Zhang.Victor Prisacariu 2. 发表年份:2019 3. 发表期刊:CVPR 4. 关键词:立体匹配.CNN.端到端训练.半全局匹配.聚 ...

  9. 【论文简述及翻译】RAFT-Stereo: Multilevel Recurrent Field Transforms for Stereo Matching(3DV 2021)

    一.论文简述 1. 第一作者:Lahav Lipson 2. 发表年份:2021 3. 发表期刊:3DV,Best paper 4. 关键词:立体匹配.端到端训练.迭代优化.GRU 5. 探索动机:立 ...

最新文章

  1. databinding 入门 知识 给TextView 赋值
  2. Keystone v3 domains 应用场景
  3. 20、Power Query-数据合并、拆分
  4. 软件工程概论 课堂练习【用例图——客户提出申请要求贷款...】
  5. 想满分的看这里:1015 德才论 (25分)
  6. 我为什么会选择计算机专业之 《我的编程人生前传》
  7. 火山安卓文件名类操作
  8. 基于VC 6.0实现简单的微信打飞机
  9. 软件测试——课程感想
  10. android 禁止第三方相机,谷歌突然宣布,Android 11推出新规,第三方相机软件猝不及防...
  11. Linux怎么把硬盘ex2改为ex4,linux磁盘管理和文件系统创建
  12. 第三届全国大学生算法设计与编程挑战赛 (冬季赛)部分题解
  13. 某易—将军令动态刨析算法(1)
  14. 智能化场景识别,8个视频图像处理方案你值得拥有
  15. java银联在线支付开发_银联在线支付案例代码
  16. python 调用接口
  17. English trip EM2-PE 3B Teacher:Olivia
  18. 电商平台之争,京东是如何脱颖而出的
  19. 关于上海物流软件产业的探讨
  20. 矩阵的压缩存储(随机稀疏矩阵的建立和输出)

热门文章

  1. T-SQL之公用表表达式(CTE)
  2. [文摘20080428]无线局域网的相关网络安全技术应用指南
  3. GitHub 上最受欢迎的 5 大 Java 项目
  4. 非985/211毕业的我,该如何在三年内月入过万?
  5. iNand、eMMC、SD、MMC的关联
  6. 除阿里、网易和字节外,杭州居然还有这么多互联网公司!
  7. MacBook写代码真香!包邮送一台!
  8. 程序员千万不要做舔狗啊!
  9. 2W 字详解设计模式!
  10. 阿里云服务器被挖矿minerd入侵的解决办法,导致tomcat启动很慢