给定一个字符串str 。任务是找到最长的子字符串,该子字符串是给定字符串str的前缀,后缀和子字符串。如果不存在这样的字符串,则打印-1 。
例子:
Input: str = “fixprefixsuffix”
Output: fix
“fix” is a prefix, suffix and present inside in the string too.
Input: str = “aaaa”
“aa” is a prefix, suffix and present inside the string.
方法:让我们为字符串的所有前缀计算最长的前缀后缀。最长前缀后缀lps [i]是前缀的最大长度,也是子字符串[0…i]的后缀。关于最长前缀后缀的更多信息,您可以在kmp算法的描述中看到。
第一个可能的答案是长度为lps [n-1]的前缀。如果lps [n-1] = 0,则没有解决方案。为了检查第一个可能的答案,您应该遍历lps [i]。如果其中至少有一个等于lps [n-1](当然不是n-1th)–您找到了答案。第二个可能的答案是长度lps [lps [n-1] -1]的前缀。如果lps [lps [n-1] -1] = 0,那么您也没有解决方案。否则,您可以确保已经找到答案。此子字符串是我们字符串的前缀和后缀。另外,它是长度为lps [n-1]的前缀的后缀,该前缀位于所有字符串的内部。该解决方案适用于O(n)。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find longest prefix suffix
vector compute_lps(string s)
{
int n = s.size();
// To store longest prefix suffix
vector lps(n);
// Length of the previous
// longest prefix suffix
int len = 0;
// lps[0] is always 0
lps[0] = 0;
int i = 1;
// Loop calculates lps[i] for i = 1 to n - 1
while (i < n) {
if (s[i] == s[len]) {
len++;
lps[i] = len;
i++;
}
// (pat[i] != pat[len])
else {
if (len != 0)
len = lps[len - 1];
// Also, note that we do not increment
// i here
// If len = 0
else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to find the longest substring
// which is prefix as well as a
// sub-string of s[1...n-2]
void Longestsubstring(string s)
{
// Find longest prefix suffix
vector lps = compute_lps(s);
int n = s.size();
// If lps of n-1 is zero
if (lps[n - 1] == 0) {
cout << -1;
return;
}
for (int i = 0; i < n - 1; i++) {
// At any position lps[i] equals to lps[n - 1]
if (lps[i] == lps[n - 1]) {
cout << s.substr(0, lps[i]);
return;
}
}
// If answer is not possible
if (lps[lps[n - 1] - 1] == 0)
cout << -1;
else
cout << s.substr(0, lps[lps[n - 1] - 1]);
}
// Driver code
int main()
{
string s = "fixprefixsuffix";
// function call
Longestsubstring(s);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to find longest prefix suffix
static int [] compute_lps(String s)
{
int n = s.length();
// To store longest prefix suffix
int [] lps = new int [n];
// Length of the previous
// longest prefix suffix
int len = 0;
// lps[0] is always 0
lps[0] = 0;
int i = 1;
// Loop calculates lps[i] for i = 1 to n - 1
while (i < n)
{
if (s.charAt(i) == s.charAt(len))
{
len++;
lps[i] = len;
i++;
}
// (pat[i] != pat[len])
else
{
if (len != 0)
len = lps[len - 1];
// Also, note that we do not increment
// i here
// If len = 0
else
{
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to find the longest substring
// which is prefix as well as a
// sub-string of s[1...n-2]
static void Longestsubstring(String s)
{
// Find longest prefix suffix
int [] lps = compute_lps(s);
int n = s.length();
// If lps of n-1 is zero
if (lps[n - 1] == 0)
{
System.out.println(-1);
return;
}
for (int i = 0; i < n - 1; i++)
{
// At any position lps[i] equals to lps[n - 1]
if (lps[i] == lps[n - 1])
{
System.out.println(s.substring(0, lps[i]));
return;
}
}
// If answer is not possible
if (lps[lps[n - 1] - 1] == 0)
System.out.println(-1);
else
System.out.println(s.substring(0, lps[lps[n - 1] - 1]));
}
// Driver code
public static void main (String [] args)
{
String s = "fixprefixsuffix";
// function call
Longestsubstring(s);
}
}
// This code is contributed by ihritik
Python3
# Python3 implementation of the approach
# Function to find longest prefix suffix
def compute_lps(s):
n = len(s)
# To store longest prefix suffix
lps = [0 for i in range(n)]
# Length of the previous
# longest prefix suffix
Len = 0
# lps[0] is always 0
lps[0] = 0
i = 1
# Loop calculates lps[i] for i = 1 to n - 1
while (i < n):
if (s[i] == s[Len]):
Len += 1
lps[i] = Len
i += 1
# (pat[i] != pat[Len])
else:
if (Len != 0):
Len = lps[Len - 1]
# Also, note that we do not increment
# i here
# If Len = 0
else:
lps[i] = 0
i += 1
return lps
# Function to find the longest substring
# which is prefix as well as a
# sub-of s[1...n-2]
def Longestsubstring(s):
# Find longest prefix suffix
lps = compute_lps(s)
n = len(s)
# If lps of n-1 is zero
if (lps[n - 1] == 0):
print(-1)
exit()
for i in range(0,n - 1):
# At any position lps[i] equals to lps[n - 1]
if (lps[i] == lps[n - 1]):
print(s[0:lps[i]])
exit()
# If answer is not possible
if (lps[lps[n - 1] - 1] == 0):
print(-1)
else:
print(s[0:lps[lps[n - 1] - 1]])
# Driver code
s = "fixprefixsuffix"
# function call
Longestsubstring(s)
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to find longest prefix suffix
static int [] compute_lps(string s)
{
int n = s.Length;
// To store longest prefix suffix
int [] lps = new int [n];
// Length of the previous
// longest prefix suffix
int len = 0;
// lps[0] is always 0
lps[0] = 0;
int i = 1;
// Loop calculates lps[i] for i = 1 to n - 1
while (i < n)
{
if (s[i] == s[len])
{
len++;
lps[i] = len;
i++;
}
// (pat[i] != pat[len])
else
{
if (len != 0)
len = lps[len - 1];
// Also, note that we do not increment
// i here
// If len = 0
else
{
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to find the longest substring
// which is prefix as well as a
// sub-string of s[1...n-2]
static void Longestsubstring(string s)
{
// Find longest prefix suffix
int [] lps = compute_lps(s);
int n = s.Length;
// If lps of n-1 is zero
if (lps[n - 1] == 0)
{
Console.WriteLine(-1);
return;
}
for (int i = 0; i < n - 1; i++)
{
// At any position lps[i] equals to lps[n - 1]
if (lps[i] == lps[n - 1])
{
Console.WriteLine(s.Substring(0, lps[i]));
return;
}
}
// If answer is not possible
if (lps[lps[n - 1] - 1] == 0)
Console.WriteLine(-1);
else
Console.WriteLine(s.Substring(0, lps[lps[n - 1] - 1]));
}
// Driver code
public static void Main ()
{
string s = "fixprefixsuffix";
// function call
Longestsubstring(s);
}
}
// This code is contributed by ihritik
PHP
输出:
fix