当前位置:网站首页>Leetcode 686. KMP method of repeatedly superimposing strings (implemented in C language)
Leetcode 686. KMP method of repeatedly superimposing strings (implemented in C language)
2022-07-29 04:23:00 【。 Little yeah】
Title Description :
Given two strings a and b, Look for overlapping strings a The minimum number of times , Make string b Becomes the superimposed string a The string of , Returns if it does not exist -1.
Be careful : character string "abc" Repeat stack 0 Next is "", Repeat stack 1 Next is "abc", Repeat stack 2 Next is "abcabc".
Example 1:
Input :a = "abcd", b = "cdabcdab"
Output :3
explain :a Repeat three times to get "abcdabcdabcd", here b It's a substring .
Example 2:
Input :a = "a", b = "aa"
Output :2
Example 3:
Input :a = "a", b = "a"
Output :1
Example 4:
Input :a = "abc", b = "wxyz"
Output :-1
Tips :
1 <= a.length <= 104
1 <= b.length <= 104
a and b It's made up of lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/repeated-string-match
Code :
void Getnext(int* next, char* sub)
{
int len = strlen(sub);
next[0] = -1;
next[1] = 0;
int i = 2;// from next[2] To traverse the
int k = 0;
while (i < len)
{
//i Go straight ahead ,k You can change
if ((k == -1) || (sub[k] == sub[i - 1]))// Because the whole array adds one to the right
{
// When 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)// stay str Match sub, If successful, return the serial number , Failure to return -1
{
//str Main string ,sub For substrings
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 when 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;
}边栏推荐
- Common components of solder pad (2021.4.6)
- (.*?) regular expression
- C语言:结构体简单语法总结
- Database SQL statement realizes function query of data decomposition
- Differences and principles of bio, NiO and AIO
- Target detection learning process
- Not for 61 days. The shortest word code
- No, just stick to it for 59 days
- LeetCode_ Stack topics
- [material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)
猜你喜欢

6.pytest生成allure报告

Can you really write restful API?

WebRTC实现简单音视频通话功能

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

Record of problems encountered in ROS learning

不会就坚持67天吧 平方根

不会就坚持68天吧 狒狒吃香蕉

Deep learning training strategy -- warming up the learning rate

The third ACM program design competition of Wuhan University of Engineering

不会就坚持66天吧 权重生成随机数
随机推荐
MySQL - clustered index and secondary index
Shielding ODBC load balancing mode in gbase 8A special scenarios?
Record of problems encountered in ROS learning
Use of torch.optim optimizer in pytorch
Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)
What is the difference between field, variable and property
visio画网格
Labelme cannot open the picture
Unity基础(3)—— unity中的各种坐标系
C language: talking about various complex statements
通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
读懂 互联网巨头 【中台之战】 以及 中台 发展思维
10.回退消息
不会就坚持64天吧 查找插入位置
Won't you insist on 71 days? List sorting
Incubator course design (April 12, 2021)
不会就坚持68天吧 狒狒吃香蕉
不会就坚持70天吧 数组中第k大的数
Visio draw grid
Definition and implementation of stack and queue (detailed)