原文:
给定一个字符串 str ,任务是找到给定字符串的最长回文前缀。
示例:
输入: str = "abaac" 输出: aba 解释: 给定回文字符串的最长前缀是“aba”。
输入: str = "abacabaxyz" 输出: abacaba 解释: 给定回文字符串的前缀是“aba”和“abacabaxyz”。 但其中最长的是“abacabaxyz”。
天真方法:思路是的所有子串,检查子串是否回文。最大长度的回文字符串就是结果字符串。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to find the longest prefix
// which is palindromic
void longestpalindromicprefix(string s)
{
// find the length of the given string
int n = s.length();
// for storing the length of longest
// prefix palindrome
int max_len = 0;
// loop to check the substring of all
// length from 1 to n which is palindrome
for (int len = 1; len <= n; len ) {
// string of length i
string temp = s.substr(0, len);
// to store the reversed of temp
string temp2 = temp;
// reversing string temp2
reverse(temp2.begin(), temp2.end());
// if string temp is palindromic
// then update the length
if (temp == temp2) {
max_len = len;
}
}
// print the palindromic string of
// max_len
cout << s.substr(0, max_len);
}
// driver code
int main()
{
// given string
string str = "abaab";
// function call
longestpalindromicprefix(str);
}
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
import java.util.*;
class gfg{
// function to find the longest prefix
// which is palindromic
static void longestpalindromicprefix(string s)
{
// find the length of the given string
int n = s.length();
// for storing the length of longest
// prefix palindrome
int max_len = 0;
// loop to check the substring of all
// length from 1 to n which is palindrome
for (int len = 1; len <= n; len )
{
// string of length i
string temp = s.substring(0, len);
// to store the reversed of temp
string temp2 = temp;
// reversing string temp2
temp2 = reverse(temp2);
// if string temp is palindromic
// then update the length
if (temp.equals(temp2))
{
max_len = len;
}
}
// print the palindromic string of
// max_len
system.out.print(s.substring(0, max_len));
}
static string reverse(string input)
{
char[] a = input.tochararray();
int l, r = a.length - 1;
for (l = 0; l < r; l , r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return string.valueof(a);
}
// driver code
public static void main(string[] args)
{
// given string
string str = "abaab";
// function call
longestpalindromicprefix(str);
}
}
// this code is contributed by rajput-ji
python 3
# python3 program for the above approach
# function to find the longest prefix
# which is palindrome
def longestpalindromicprefix(string):
# find the length of the given string
n = len(string)
# for storing the length of longest
# prefix palindrome
max_len = 0
# loop to check the substring of all
# length from 1 to n which is palindrome
for length in range(0, n 1):
# string of length i
temp = string[0:length]
# to store the value of temp
temp2 = temp
# reversing the value of temp
temp3 = temp2[::-1]
# if string temp is palindromic
# then update the length
if temp == temp3:
max_len = length
# print the palindromic string
# of max_len
print(string[0:max_len])
# driver code
if __name__ == '__main__' :
string = "abaac";
# function call
longestpalindromicprefix(string)
# this code is contributed by virusbuddah_
c
// c# program for the above approach
using system;
class gfg{
// function to find the longest prefix
// which is palindromic
static void longestpalindromicprefix(string s)
{
// find the length of the given string
int n = s.length;
// for storing the length of longest
// prefix palindrome
int max_len = 0;
// loop to check the substring of all
// length from 1 to n which is palindrome
for (int len = 1; len <= n; len )
{
// string of length i
string temp = s.substring(0, len);
// to store the reversed of temp
string temp2 = temp;
// reversing string temp2
temp2 = reverse(temp2);
// if string temp is palindromic
// then update the length
if (temp.equals(temp2))
{
max_len = len;
}
}
// print the palindromic string of
// max_len
console.write(s.substring(0, max_len));
}
static string reverse(string input)
{
char[] a = input.tochararray();
int l, r = a.length - 1;
for (l = 0; l < r; l , r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return string.join("",a);
}
// driver code
public static void main(string[] args)
{
// given string
string str = "abaab";
// function call
longestpalindromicprefix(str);
}
}
// this code is contributed by amal kumar choubey
java 描述语言
output:
aba
时间复杂度: o(n 2 ) ,其中 n 为给定字符串的长度。
高效途径:思路是使用预处理算法 。以下是步骤:
- 创建一个临时字符串(比如 str2 ),它是:
str2 = str '?' reverse(str);
- 创建一个字符串长度大小的数组(比如 lps[] ),该数组将存储最长的回文前缀,该前缀也是字符串的后缀 str2 。
- 使用 的预处理算法更新 lps[]。
- lps[length(str 2)–1]将给出给定字符串字符串的最长回文前缀字符串的长度。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to find the longest prefix
// which is palindromic
void longestpalindromicprefix(string str)
{
// create temporary string
string temp = str '?';
// reverse the string str
reverse(str.begin(), str.end());
// append string str to temp
temp = str;
// find the length of string temp
int n = temp.length();
// lps[] array for string temp
int lps[n];
// initialise every value with zero
fill(lps, lps n, 0);
// iterate the string temp
for (int i = 1; i < n; i ) {
// length of longest prefix
// till less than i
int len = lps[i - 1];
// calculate length for i 1
while (len > 0
&& temp[len] != temp[i]) {
len = lps[len - 1];
}
// if character at current index
// len are same then increment
// length by 1
if (temp[i] == temp[len]) {
len ;
}
// update the length at current
// index to len
lps[i] = len;
}
// print the palindromic string of
// max_len
cout << temp.substr(0, lps[n - 1]);
}
// driver's code
int main()
{
// given string
string str = "abaab";
// function call
longestpalindromicprefix(str);
}
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
import java.util.*;
class gfg{
// function to find the longest
// prefix which is palindromic
static void longestpalindromicprefix(string str)
{
// create temporary string
string temp = str '?';
// reverse the string str
str = reverse(str);
// append string str to temp
temp = str;
// find the length of string temp
int n = temp.length();
// lps[] array for string temp
int []lps = new int[n];
// initialise every value with zero
arrays.fill(lps, 0);
// iterate the string temp
for(int i = 1; i < n; i )
{
// length of longest prefix
// till less than i
int len = lps[i - 1];
// calculate length for i 1
while (len > 0 && temp.charat(len) !=
temp.charat(i))
{
len = lps[len - 1];
}
// if character at current index
// len are same then increment
// length by 1
if (temp.charat(i) == temp.charat(len))
{
len ;
}
// update the length at current
// index to len
lps[i] = len;
}
// print the palindromic string
// of max_len
system.out.print(temp.substring(0, lps[n - 1]));
}
static string reverse(string input)
{
char[] a = input.tochararray();
int l, r = a.length - 1;
for(l = 0; l < r; l , r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return string.valueof(a);
}
// driver code
public static void main(string[] args)
{
// given string
string str = "abaab";
// function call
longestpalindromicprefix(str);
}
}
// this code is contributed by rajput-ji
python 3
# python3 program for the above approach
# function to find the longest prefix
# which is palindromic
def longestpalindromicprefix(str):
# create temporary string
temp = str "?"
# reverse the string str
str = str[::-1]
# append string str to temp
temp = temp str
# find the length of string temp
n = len(temp)
# lps[] array for string temp
lps = [0] * n
# iterate the string temp
for i in range(1, n):
# length of longest prefix
# till less than i
len = lps[i - 1]
# calculate length for i 1
while (len > 0 and temp[len] != temp[i]):
len = lps[len - 1]
# if character at current index
# len are same then increment
# length by 1
if (temp[i] == temp[len]):
len = 1
# update the length at current
# index to len
lps[i] = len
# print the palindromic string
# of max_len
print(temp[0 : lps[n - 1]])
# driver code
if __name__ == '__main__':
# given string
str = "abaab"
# function call
longestpalindromicprefix(str)
# this code is contributed by himanshu77
c
// c# program for the above approach
using system;
class gfg{
// function to find the longest
// prefix which is palindromic
static void longestpalindromicprefix(string str)
{
// create temporary string
string temp = str '?';
// reverse the string str
str = reverse(str);
// append string str to temp
temp = str;
// find the length of string temp
int n = temp.length;
// lps[] array for string temp
int []lps = new int[n];
// iterate the string temp
for(int i = 1; i < n; i )
{
// length of longest prefix
// till less than i
int len = lps[i - 1];
// calculate length for i 1
while (len > 0 && temp[len] != temp[i])
{
len = lps[len - 1];
}
// if character at current index
// len are same then increment
// length by 1
if (temp[i] == temp[len])
{
len ;
}
// update the length at current
// index to len
lps[i] = len;
}
// print the palindromic string
// of max_len
console.write(temp.substring(0, lps[n - 1]));
}
static string reverse(string input)
{
char[] a = input.tochararray();
int l, r = a.length - 1;
for(l = 0; l < r; l , r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return string.join("", a);
}
// driver code
public static void main(string[] args)
{
// given string
string str = "abaab";
// function call
longestpalindromicprefix(str);
}
}
// this code is contributed by rajput-ji
java 描述语言
output:
aba
时间复杂度: o(n) ,其中 n 为给定字符串的长度。 辅助空间: o(n) ,其中 n 为给定字符串的长度。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处