当前位置:网站首页>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;
}边栏推荐
- kotlin的List,Map,Set等集合类不指定类型
- How to solve the problem of store ranking?
- How to query the submission number of a version
- 读懂 互联网巨头 【中台之战】 以及 中台 发展思维
- C语言力扣第61题之旋转链表。双端队列与构造循环链表
- MySQL gets the maximum value record by field grouping
- Won't you just stick to 62 days? Sum of words
- 不会就坚持61天吧 最短的单词编码
- Exception resolution: error of not finding edu.stanford.nlp.semgraph.semgrex.semgrexpattern in cococaption package
- Opengauss pre check installation
猜你喜欢
随机推荐
11.备份交换机
6. Pytest generates an allure Report
不会就坚持67天吧 平方根
C language: summary of consortium knowledge points
How to solve the problem of store ranking?
Record of problems encountered in ROS learning
不会就坚持59天吧 替换单词
Mpc5744p introduction and opensda firmware update
信号处理中的反傅里叶变换(IFFT)原理
Can you really write restful API?
Openfeign asynchronous call problem
不会就坚持65天吧 只出现一次的数字
Why are there so many unknowns when opengauss starts?
How to query the submission number of a version
openFeign异步调用问题
RMAN do not mark expired backups
Model tuning, training model trick
Visio draw grid
Back propagation process of manual BP neural network
9.延迟队列









