当前位置:网站首页>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;
}
边栏推荐
- C语言:typedef知识点总结
- Won't you just stick to 69 days? Merge range
- Not for 63 days. The biggest XOR
- What is the use of meta-info?
- Deep learning training strategy -- warming up the learning rate
- 不会就坚持58天吧 实现前缀树
- 14.haproxy+keepalived负载均衡和高可用
- Shielding ODBC load balancing mode in gbase 8A special scenarios?
- 12.优先级队列和惰性队列
- Don't insist on 66 days. Weight generates random numbers
猜你喜欢
不会就坚持64天吧 查找插入位置
Unity基础(3)—— unity中的各种坐标系
Won't you just stick to 62 days? Sum of words
MySQL - deep parsing of MySQL index data structure
Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
不会就坚持61天吧 最短的单词编码
10. Fallback message
Class starts! See how smardaten decomposes complex business scenarios
Deep learning training strategy -- warming up the learning rate
Not for 58 days. Implement prefix tree
随机推荐
10. Fallback message
Exception handling: pyemd or pyemd not found
Niuke IOI weekly 27 popularity group
14.haproxy+keepalived负载均衡和高可用
索引的最左前缀原理
Wechat applet parameter transfer
10.回退消息
What is the difference between field, variable and property
Basic operation of queue
Use of torch.optim optimizer in pytorch
Down sampling and up sampling
不会就坚持67天吧 平方根
Incubator course design (April 12, 2021)
Why are there so many unknowns when opengauss starts?
Don't stick to it for 68 days. Baboons eat bananas
Deep learning training strategy -- warming up the learning rate
Installation and use of stm32cubemx (5.3.0)
不会就坚持63天吧 最大的异或
你真的会写Restful API吗?
Labelme cannot open the picture