Longest Palindrome in a String
PROBLEM :
Given a string S, find the longest palindromic substring in S.
Substring of string S:
S[ i . . . . j ] where 0 = i = j < len(S)
Palindrome string:
A string which reads the same backwards. More formally, S is palindrome if reverse(S) = S.
Incase of conflict, return the substring which occurs first ( with the least starting index ).
Input:
The first line of input consists number of the test cases. The following T lines consist of a string each.
Output:
In each separate line print the longest palindrome of the string given in the respective test case.
Constraints:
1 =T= 100
1 = str= 100
Example:
Input:
1
aaaabbaa
Output:
aabbaa
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : DP solution O(n^2)
--------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
string GetSubString(string str,int start,int end){
if(start==end)
return "" ;
string s="" ;
for(int i=start;i<=end;i++)
s=s+str[i] ;
return s ;
}
string longestPalindrome(string str) {
if(str.length()==0)
return "" ;
string STR=str ;
bool dp[str.length()][str.length()] ;
string palindromSTR ;
int i,j,k ;
for(i=0;i<str.length();i++)
for(j=0;j<str.length();j++)
dp[i][j]=false ;
for(i=0;i<str.length();i++)
dp[i][i]=true ;
palindromSTR=str[0] ;
for(i=0;i<str.length()-1;i++)
if(str[i]==str[i+1]){
dp[i][i+1]=true ;
if(GetSubString(str,i,i+1).length()>palindromSTR.length())
palindromSTR=GetSubString(str,i,i+1) ;
}
for(k=2;k<str.length();k++){
for(i=0;i<str.length()-k;i++){
j=i+k ;
if(str[i]==str[j]&&dp[i+1][j-1]==true){
if(GetSubString(str,i,j).length()>palindromSTR.length())
palindromSTR=GetSubString(str,i,j) ;
dp[i][j]=true ;
}
}
}
return palindromSTR ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
string str ;
cin>>str ;
cout<<longestPalindrome(str)<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : O(n^2)
--------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
void PrintString(string str,int start,int end){
for(int i=start;i<=end;i++)
cout<<str[i] ;
cout<<endl ;
}
void longestPalindrome(string str) {
int start=0;
int maxlen=1 ;
int len=str.length() ;
int low,high ;
for(int i=1;i<len;i++)
{
low=i-1 ;
high=i ;
while(low>=0&&high<len && str[low]==str[high])
{
if(high-low+1>maxlen)
{
start=low ;
maxlen=high-low+1 ;
}
low-- ;
high++ ;
}
low=i-1 ;
high=i+1 ;
while(low>=0&&high<len && str[low]==str[high])
{
if(high-low+1>maxlen)
{
start=low ;
maxlen=high-low+1 ;
}
low-- ;
high++ ;
}
}
PrintString(str,start,start+maxlen-1) ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
string str ;
cin>>str ;
longestPalindrome(str) ;
}
return 0;
}
---------------------------------------------------------------------------------
Given a string S, find the longest palindromic substring in S.
Substring of string S:
S[ i . . . . j ] where 0 = i = j < len(S)
Palindrome string:
A string which reads the same backwards. More formally, S is palindrome if reverse(S) = S.
Incase of conflict, return the substring which occurs first ( with the least starting index ).
Input:
The first line of input consists number of the test cases. The following T lines consist of a string each.
Output:
In each separate line print the longest palindrome of the string given in the respective test case.
Constraints:
1 =T= 100
1 = str= 100
Example:
Input:
1
aaaabbaa
Output:
aabbaa
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : DP solution O(n^2)
--------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
string GetSubString(string str,int start,int end){
if(start==end)
return "" ;
string s="" ;
for(int i=start;i<=end;i++)
s=s+str[i] ;
return s ;
}
string longestPalindrome(string str) {
if(str.length()==0)
return "" ;
string STR=str ;
bool dp[str.length()][str.length()] ;
string palindromSTR ;
int i,j,k ;
for(i=0;i<str.length();i++)
for(j=0;j<str.length();j++)
dp[i][j]=false ;
for(i=0;i<str.length();i++)
dp[i][i]=true ;
palindromSTR=str[0] ;
for(i=0;i<str.length()-1;i++)
if(str[i]==str[i+1]){
dp[i][i+1]=true ;
if(GetSubString(str,i,i+1).length()>palindromSTR.length())
palindromSTR=GetSubString(str,i,i+1) ;
}
for(k=2;k<str.length();k++){
for(i=0;i<str.length()-k;i++){
j=i+k ;
if(str[i]==str[j]&&dp[i+1][j-1]==true){
if(GetSubString(str,i,j).length()>palindromSTR.length())
palindromSTR=GetSubString(str,i,j) ;
dp[i][j]=true ;
}
}
}
return palindromSTR ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
string str ;
cin>>str ;
cout<<longestPalindrome(str)<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : O(n^2)
--------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
void PrintString(string str,int start,int end){
for(int i=start;i<=end;i++)
cout<<str[i] ;
cout<<endl ;
}
void longestPalindrome(string str) {
int start=0;
int maxlen=1 ;
int len=str.length() ;
int low,high ;
for(int i=1;i<len;i++)
{
low=i-1 ;
high=i ;
while(low>=0&&high<len && str[low]==str[high])
{
if(high-low+1>maxlen)
{
start=low ;
maxlen=high-low+1 ;
}
low-- ;
high++ ;
}
low=i-1 ;
high=i+1 ;
while(low>=0&&high<len && str[low]==str[high])
{
if(high-low+1>maxlen)
{
start=low ;
maxlen=high-low+1 ;
}
low-- ;
high++ ;
}
}
PrintString(str,start,start+maxlen-1) ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
string str ;
cin>>str ;
longestPalindrome(str) ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment