当前位置:网站首页>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;
}边栏推荐
- 不会就坚持62天吧 单词之和
- C语言:浅谈各种复杂的声明
- 15.federation
- “蔚来杯“2022牛客暑期多校训练营2 H
- 10. Fallback message
- MySQL gets the maximum value record by field grouping
- Machine vision Series 2: vs DLL debugging
- Introduction and examples of parameters in Jenkins parametric construction
- It won't last for 65 days. It only appears once
- Taobao product details interface (product details page data interface)
猜你喜欢

It won't last for 65 days. It only appears once

HC06 HC05 BT

MPU6050

Two forms of softmax cross entropy + numpy implementation

Compilation and linking

The principle of inverse Fourier transform (IFFT) in signal processing

Why are there so many unknowns when opengauss starts?

Record of problems encountered in ROS learning

RMAN do not mark expired backups

不会就坚持69天吧 合并区间
随机推荐
不会就坚持61天吧 最短的单词编码
12. Priority queue and inert queue
顺序表和链表
Code or script to speed up the video playback of video websites
淘宝商品详情接口(商品详情页面数据接口)
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
Incubator course design (April 12, 2021)
15.federation
一个公司的面试笔记
How to execute insert into select from job in SQL client
2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
How to set the SQL execution timeout for flick SQL
Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)
The difference between dynamic, VaR and object in fluent
Use of torch.optim optimizer in pytorch
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
Exception handling: pyemd or pyemd not found
kotlin的List,Map,Set等集合类不指定类型
不会就坚持68天吧 狒狒吃香蕉
不会就坚持69天吧 合并区间