当前位置:网站首页>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;
}边栏推荐
- BIO、NIO、AIO的区别和原理
- Deploy Jenkins using containers
- It won't last for 70 days. The k-largest number in the array
- Flutter实战-请求封装(二)之dio
- [hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
- 一个公司的面试笔记
- Laya中的A星寻路
- C language: summary of consortium knowledge points
- 12. Priority queue and inert queue
- C语言:typedef知识点总结
猜你喜欢

Won't you just stick to 62 days? Sum of words

不会就坚持58天吧 实现前缀树

TypeError: Cannot read properties of undefined (reading ‘then‘)

Svg -- loading animation

Basic operation of queue

Applet: Area scrolling, pull-down refresh, pull-up load more

通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值

Common components of solder pad (2021.4.6)

Whole house WiFi solution: mesh router networking and ac+ap

编译与链接
随机推荐
Shell string segmentation
Don't insist on 66 days. Weight generates random numbers
Database SQL statement realizes function query of data decomposition
Fuzzy query of SQL
No, just stick to it for 64 days. Find the insertion location
HC06 HC05 BT
不会就坚持71天吧 链表排序
String, array, generalized table (detailed)
14.haproxy+keepalived负载均衡和高可用
Pytoch distributed training
Applet: Area scrolling, pull-down refresh, pull-up load more
C语言:typedef知识点总结
Pytorch GPU and CPU models load each other
Common components of solder pad (2021.4.6)
Why are there so many unknowns when opengauss starts?
Won't you just stick to 62 days? Sum of words
SQL time fuzzy query datediff() function
settings.xml
Code or script to speed up the video playback of video websites
开课!看smardaten如何分解复杂业务场景