Ejemplo n.º 1
0
int longestPalindromicSubstring(char *s){
  int n = strlen(s);
  int maxlength = 1, start = 0;
  bool table[n][n];                  // maintain a table n x n, where string length is n. table[i][j] says string[i] = string[j]
  memset(table, 0, sizeof(table));
  for(int i = 0; i < n; i++)
    table[i][i] = true;              // put each entry of table where i == j as true           
  for(int i = 0; i < (n - 1); ++i){
    if(s[i] == s[i + 1]){            // if consecutive entries are same then they are palindromes of length 2
      start = i;                     
      maxlength = 2;                 
      table[i][i + 1] = true;
    }
  }// table now contains all i,j as true, where str[i] == s[j], that is positions of all 2 length palindromes are stored     
  for(int k = 3; k <= n; ++k){       
    for(int i = 0; i < n - k + 1; ++i){  // i started from 0
      int j = i + k - 1; // j started from 2(search now 3 length palindrom substrings), we have start-end pos of 2 already                        
      if(table[i + 1][j - 1] && s[i] == s[j]){// if str[i] == str[j] and table[i + 1][j - 1] = true, table[i][j] = true. This means that
        table[i][j] = true;                   // for k = 3, we will have start end position as true in table, for k=4 same as & so on 
        if(k > maxlength){
          start = i;
          maxlength = k; // k determines the length of the palindromic string
        }
      }
    }
  }
  printf("Longest Palindromic String : ");
  printSubStr(start, start + maxlength - 1, s);
  return maxlength;
}
// This function prints the longest palindrome substring of str[].
// It also returns the length of the longest palindrome
int longestPalSubstr( char *str )
{
    int n = strlen( str ); // get length of input string
 
    // table[i][j] will be false if substring str[i..j] is not palindrome.
    // Else table[i][j] will be true
    bool table[n][n];
    memset( table, 0, sizeof( table ) );
 
    // All substrings of length 1 are palindromes
    int maxLength = 1;
    for( int i = 0; i < n; ++i )
        table[i][i] = true;
 
    // check for sub-string of length 2.
    int start = 0;
    for( int i = 0; i < n-1; ++i )
    {
        if( str[i] == str[i+1] )
        {
            table[i][i+1] = true;
            start = i;
            maxLength = 2;
        }
    }
 
    // Check for lengths greater than 2. k is length of substring
    for( int k = 3; k <= n; ++k )
    {
        // Fix the starting index
        for( int i = 0; i < n - k + 1 ; ++i )
        {
            // Get the ending index of substring from starting index i and length k
            int j = i + k - 1;
 
            // checking for sub-string from ith index to jth index iff str[i+1]
            // to str[j-1] is a palindrome
            if( table[i+1][j-1] && str[i] == str[j] )
            {
                table[i][j] = true;
 
                if( k > maxLength )
                {
                    start = i;
                    maxLength = k;
                }
            }
        }
    }
 
    printf("Longest palindrome substring is: ");
    printSubStr( str, start, start + maxLength - 1 );
 
    return maxLength; // return length of LPS
}
Ejemplo n.º 3
0
int main(){
    char a[500][100];
    char sPRT[3400], scPRT[3400];
    int sect;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    gets(sDNA);
    gets(peptide);
    char *scDNA = new char[strlen(sDNA)];
    char *sRNA = new char[strlen(sDNA)];
    char *scRNA = new char[strlen(sDNA)];
    complement(sDNA, scDNA);
    DNAintoRNA(sDNA, sRNA);
    DNAintoRNA(scDNA, scRNA);  
    
    count = 0;
    for(pos = 0; pos < 3; pos++){
        translateRNAintoPRT(sPRT, sRNA, pos);
        KMPMatcher(sPRT, peptide);
    }
    sect = count;
    for(pos = 0; pos < 3; pos++){        
        translateRNAintoPRT(scPRT, scRNA, pos);
        KMPMatcher(scPRT, peptide);  
    }
        
    for(int i = 0; i < count; i++){
        for(int j = i + 1; j < count; j++){
            if((i < sect) && (j < sect)){
                funct(i, j, sRNA, sRNA);     
            }
            if((i < sect) && (j >= sect)){
                funct(i, j, sRNA, scRNA);     
            }
            if((i >= sect) && (j >= sect)){
                funct(i, j, scRNA, scRNA);     
            }          
        }        
    }
    
    printSubStr(sDNA, scDNA, sect);
     
return 0;    
}
Ejemplo n.º 4
0
int palindromic_string(char *str)
{
	bool table[n][n];
int i,j,max_length,start;
 memset( table, 0, sizeof( table ) );
max_length=1;
for(i=0;i<n;i++)
{
	table[i][i]=true;
}

for(i=0;i<n-1;i++)
{
	if(str[i]==str[i+1])
	{
	table[i][i+1]=true;
	start=i;
	max_length=2;
	}
}
for(k=3;k<=n;++k)
{
	for(i=0;i<n-k+1;++i)
	{
		j=i+k-1;
		if(table[i+1][j-1] && (str[i]=str[j]))
		{
			table[i][j]=true;
		}
		if(max_length<k)
		{
		max_length=k;
		start=i;
	}}
}
print(printSubStr( str, start, start + maxLength - 1 );
return max_length;
}