最长公共前缀后缀_最长的前缀和后缀

桃扇骨 2023-03-05 09:52 80阅读 0赞

最长公共前缀后缀

Problem statement:

问题陈述:

Given a string of character, find the length of longest proper prefix which is also a proper suffix.

给定一个字符串,找到最长适当前缀的长度,这也是一个适当的后缀。

Input:

输入:

First line is T number of test cases. Each test case has one line denoting the string of length less than 100000.

第一行是T个测试用例。 每个测试用例都有一行表示长度小于100000的字符串。

Output:

输出:

Print length of longest proper prefix which is also a proper suffix.

打印最长适当前缀的长度,该长度也是适当的后缀。

Examples:

例子:

  1. Input:
  2. T = 1
  3. "abcdabc"
  4. Output:
  5. 3
  6. //as prefix "abc" is the longest proper prefix
  7. //present in the string.
  8. Input:
  9. T = 1
  10. "abcdefghijklm"
  11. Output:
  12. 0
  13. //as there is no prefix which is a suffix.
  14. Input:
  15. T = 1
  16. "aaaaaaa"
  17. Output:
  18. 6
  19. //as for proper prefix we can't have entire length
  20. // as the prefix so only length possible is 6.

Solution Approach

解决方法

1) Brute Force Approach

1)蛮力法

Since the longest proper prefix which is also a suffix cannot be equal to the entire length of the string because it can’t overlap each other, so we break the string from mid-point and start matching left and right string. If they are equal return size of any one string else try for shorter lengths on both sides.

由于最长的正确前缀(也是后缀)不能等于字符串的整个长度,因为它不能相互重叠,因此我们从中点处断开字符串,然后开始匹配左右字符串。 如果它们的返回值等于任一字符串的大小,则请尝试在两侧都使用较短的长度。

Pseudo Code:

伪代码:

  1. void solve(string str)
  2. {
  3. // declare two variable which will used for
  4. // comparing two halfs of the string.
  5. int i, j;
  6. //store length in len variable.
  7. int len = str.length();
  8. i = len / 2; // initialise right half.
  9. j = 0; // initialise left half.
  10. // check if the right index variable
  11. // is less than the length of the string.
  12. while (i < len) {
  13. // if the left half character is
  14. // equal to right half characet.
  15. is(str[i] == str[j])
  16. {
  17. // simply incres the pointers to compare next indexes.
  18. i++;
  19. j++;
  20. continue;
  21. }
  22. else
  23. {
  24. // if the left half character is at start index
  25. // and it is not equal to right half current
  26. // character then increase
  27. if (j == 0) { // right half index.
  28. i++;
  29. }
  30. else { //search for lesser length of the prefix.
  31. j--;
  32. }
  33. }
  34. }
  35. return j; //return length of prefix.
  36. }

C++ Implementation:

C ++实现:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int main()
  5. {
  6. ll t;
  7. cout << "Enter number of test cases: ";
  8. cin >> t;
  9. while (t--) {
  10. cout << "Enter string: ";
  11. string str;
  12. cin >> str;
  13. ll len = str.length();
  14. // if length is less than one then
  15. // simply length of lps is 0.
  16. if (len <= 1) {
  17. cout << 0 << "\n";
  18. }
  19. else {
  20. ll i = len / 2; // initialise right half.
  21. ll j = 0; // initialise left half.
  22. // check if the right index variable is less
  23. // than the length of the string.
  24. while (i < len) {
  25. // if the left half character is equal to
  26. // right half characet.
  27. if (str[i] == str[j]) {
  28. i++;
  29. j++;
  30. }
  31. else {
  32. // if the left half character is at start index
  33. // and it is not equal to right half current
  34. // character then increase right half index.
  35. if (j == 0)
  36. i++;
  37. else
  38. j--; // search for lesser length of the prefix
  39. }
  40. }
  41. cout << "Longest prefix-suffix length: ";
  42. cout << j << "\n";
  43. }
  44. }
  45. return 0;
  46. }

Output

输出量

  1. Enter number of test cases: 3
  2. Enter string: abcdabc
  3. Longest prefix-suffix length: 3
  4. Enter string: abcdefghabcdefgh
  5. Longest prefix-suffix length: 8
  6. Enter string: abcdefgh
  7. Longest prefix-suffix length: 0

2) Better approach: Using longest prefix-suffix array of KMP algorithm

2)更好的方法:使用最长的KMP算法前缀-后缀数组

Here we will use an array which represents the length of the longest proper prefix which is also equal to the suffix of the array.

在这里,我们将使用一个数组,该数组代表最长适当前缀的长度,该长度也等于数组的后缀。

Here we use lps[] array which stores the length of a longest proper prefix which is the equal suffix, each index of lps represent the current index lps upto that index comparison, initialize lps[0]=0, since it is not possible to have proper prefix-suffix with one letter then we declare two variable j and i by which we compare the characters.

在这里,我们使用lps []数组,该数组存储最长的适当前缀(等于后缀)的长度,每个lps索引代表直到该索引比较为止的当前索引lps ,初始化lps [0] = 0 ,因为不可能有一个带一个字母的适当前缀后缀,然后我们声明两个变量ji,通过它们我们比较字符。

Pseudo Code:

伪代码:

  1. int longestprefix(string str)
  2. {
  3. int len = str.length(); //len variable stores length of string.
  4. int lps[len]; //declare lps array.
  5. lps[0] = 0; //single charater case so length lps is 0.
  6. int i = 1; //initialise index for comparison from 1.
  7. int j = 0; //initial longest prefix suffix length.
  8. while (i < len) //check until i!=len of string.
  9. {
  10. //check current index character with character stored at j.
  11. if (str[i] == str[j]) {
  12. j++; //increase length by one.
  13. lps[i] = j; //lps for current index is equal to j
  14. i++; //move forward.
  15. }
  16. else //if str[i]!=str[j]
  17. {
  18. //we search for that index which character matches
  19. //with the current character at index i.
  20. if (j != 0) {
  21. j = lps[j - 1];
  22. }
  23. else {
  24. lps[i] = 0;
  25. i++;
  26. } //if j==0 then lps[i]=0.
  27. }
  28. }
  29. return lps[j - 1]; //return lcs of given string.
  30. }

C++ Implementation:

C ++实现:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int main()
  5. {
  6. ll t;
  7. cout << "Enter number of test cases: ";
  8. cin >> t;
  9. while (t--) {
  10. cout << "Enter string: ";
  11. string str;
  12. cin >> str;
  13. ll len = str.length(); //len variable stores length of string.
  14. ll lps[len]; //declare lps array.
  15. lps[0] = 0; //single charater case so length lps is 0.
  16. ll i, j;
  17. i = 1; //initialise index for comparison from 1.
  18. j = 0; //initial longest prefix suffix length.
  19. while (i < len) //check until i!=len of string.
  20. {
  21. //check current index character with character stored at j.
  22. if (str[i] == str[j])
  23. {
  24. j++; //increase length by one.
  25. lps[i] = j; //lps for current index is equal to j
  26. i++; //move forward.
  27. }
  28. else //if str[i]!=str[j]
  29. {
  30. //we search for that index which character matches
  31. //with the current character at index i.
  32. if (j != 0)
  33. j = lps[j - 1];
  34. else {
  35. lps[i] = 0; //if j==0 then lps[i]=0.
  36. i++; //move forward.
  37. }
  38. }
  39. }
  40. cout << "Maximum length of proper prefix-suffix: ";
  41. cout << lps[len - 1] << "\n";
  42. }
  43. return 0;
  44. }

Output:

输出:

  1. Enter number of test cases: 3
  2. Enter string: abcdefgh
  3. Maximum length of proper prefix-suffix: 0
  4. Enter string: abcdabc
  5. Maximum length of proper prefix-suffix: 3
  6. Enter string: abcdefghabcdefgh
  7. Maximum length of proper prefix-suffix: 8
  • Time Complexity for above approach is: O(n)

    上述方法的时间复杂度为: O(n)

  • Space Complexity for above approach is: O(n)

    上述方法的空间复杂度为: O(n)



Problem reference: https://practice.geeksforgeeks.org/problems/longest-prefix-suffix/0

问题参考:https://practice.geeksforgeeks.org/problems/longest-prefix-suffix/0

翻译自: https://www.includehelp.com/icp/longest-prefix-and-suffix.aspx

最长公共前缀后缀

发表评论

表情:
评论列表 (有 0 条评论,80人围观)

还没有评论,来说两句吧...

相关阅读

    相关 公共前缀

    题目: 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 示例 1: 输入:strs = \[“flower”,“flow”

    相关 公共前缀-Java

    //最长公共前缀 //题目描述:编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。 //示例 1: //输入: \["flowe