pat_1040

    對於輸入的字符串,複製一個它的反串,求它們最大的公共子串
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

 char str[1003];
 char s2[1003];
 int dp[1005][1005];
int main()
{

  while( gets(str))
  {
  
	  int n=strlen(str);
	  for(int i=n-1;i>=0;i--)
		  s2[i]=str[n-1-i];
	 
	  dp[0][0]=0;
	  for(int l=0;l<n;l++)
		  dp[0][l]=dp[l][0]=0;
	  for(int j=1;j<=n;j++)
		  for(int k=1;k<=n;k++)
		  {
			  if(str[j-1]==s2[k-1]){dp[k][j]=dp[k-1][j-1]+1;}
		  else
			  dp[k][j]=0;
		  }
  
		  int max=1;
		  for(int u=1;u<=n;u++)
			  for(int s=1;s<=n;s++)
				  if(max<dp[u][s])max=dp[u][s];

				  cout<<max<<endl;
  }
   return 0;
}

發佈了41 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章