当前位置:网站首页>leetcode 686.重复叠加字符串 KMP方法(C语言实现)
leetcode 686.重复叠加字符串 KMP方法(C语言实现)
2022-07-29 04:20:00 【。小耶】
题目描述:
给定两个字符串 a 和 b,寻找重复叠加字符串 a 的最小次数,使得字符串 b 成为叠加后的字符串 a 的子串,如果不存在则返回 -1。
注意:字符串 "abc" 重复叠加 0 次是 "",重复叠加 1 次是 "abc",重复叠加 2 次是 "abcabc"。
示例 1:
输入:a = "abcd", b = "cdabcdab"
输出:3
解释:a 重复叠加三遍后为 "abcdabcdabcd", 此时 b 是其子串。
示例 2:
输入:a = "a", b = "aa"
输出:2
示例 3:
输入:a = "a", b = "a"
输出:1
示例 4:
输入:a = "abc", b = "wxyz"
输出:-1
提示:
1 <= a.length <= 104
1 <= b.length <= 104
a 和 b 由小写英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/repeated-string-match
代码:
void Getnext(int* next, char* sub)
{
int len = strlen(sub);
next[0] = -1;
next[1] = 0;
int i = 2;//从next[2]开始遍历
int k = 0;
while (i < len)
{
//i一直往前走,k可以变换
if ((k == -1) || (sub[k] == sub[i - 1]))//因为数组整体往右加一
{
//当sub[k] == sub[i]
//next[i+1] == k + 1
next[i] = k + 1;
i++;
k++;
}
else
{
k = next[k];
}
}
}
int kmp(char* str, char* sub)//在str中匹配sub,若成功返回序号,失败返回-1
{
//str为主串,sub为子串
if (strlen(str) < strlen(sub))
{
return -1;
}
int i = 0;
int j = 0;
int lenstr = strlen(str);
int lensub = strlen(sub);
int* next = (int*)malloc(sizeof(int) * lenstr);
Getnext(next, sub);
while (i < lenstr && j < lensub)
{
if ((j == -1) || (sub[j] == str[i]))
{
//j == -1时next[0] = -1
j++;
i++;
}
else
{
j = next[j];
}
}
free(next);
if (j >= lensub)
{
return i - j;
}
return -1;
}
int repeatedStringMatch(char * a, char * b)
{
if(strcmp(a,b)==0)
{
return 1;
}
char c[100000];
strcpy(c, a);
int i = 1;
while (kmp(c,b) ==-1 && strlen(c) <= 2 * strlen(a) + strlen(b))
{
strcat(c, a);
i++;
}
if (strlen(c) <= 2 * strlen(a) + strlen(b))
return i;
else
return -1;
}边栏推荐
- Pytorch GPU and CPU models load each other
- 如何查询版本的提交号
- Rhel8 patch package production
- rman不标记过期备份
- The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
- opengauss预检查安装
- Model tuning, training model trick
- 一个公司的面试笔记
- 全屋WiFi方案:Mesh路由器组网和AC+AP
- C language: summary of consortium knowledge points
猜你喜欢
![[kvm] create virtual machine from kickstart file](/img/0e/292ccb6862e29d948ad6ece86b7945.png)
[kvm] create virtual machine from kickstart file

Visio draw grid

Not for 58 days. Implement prefix tree

信号处理中的反傅里叶变换(IFFT)原理

Compilation and linking

不会就坚持63天吧 最大的异或

Not for 60 days, magical dictionary

Not for 63 days. The biggest XOR

How to solve the problem of store ranking?

Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
随机推荐
Multi rotor six axis hardware selection
如何查询版本的提交号
Pytoch automatic mixing accuracy (AMP) training
不会就坚持62天吧 单词之和
数据库SQL语句实现数据分解的函数查询
C language: typedef knowledge points summary
C语言:typedef知识点总结
Methods of using multiple deformations on an element
Machine vision Series 2: vs DLL debugging
15.federation
Codeforces Round #810 (Div. 2) D. Rain (线段树差分)
Not 67 days, square root
15.federation
The function "postgis_version" cannot be found when installing PostGIS
异常处理:pyemd或PyEMD找不到
不会就坚持64天吧 查找插入位置
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
6. Pytest generates an allure Report
Interview notes of a company
[kvm] create virtual machine from kickstart file