ACM PKU 3291 Cousins http://acm.pku.edu.cn/JudgeOnline/problem?id=3291

DP + 贪心 搞定 :
#include <iostream>
using namespace std;

char in_st[101], in_ed[101];
int len[101][101];

int shortest(int n,int m)
{
 int i , j;
 for (i = 0;i<=n;i++)  len[i][0] = 0;
 for (i = 0;i<=m;i++)  len[0][i] = 0;
 
 for(i = 1; i <=n; i++)
 {
  for (j = 1; j <=m ;j++)
  {
   if (in_st[i - 1] == in_ed[j - 1]) len[i][j] = len[i - 1][j-1]+1;    
   
   else if (len[i - 1][j] >= len[i][j-1]) len[i][j] = len[i - 1][j];    
   
   else len[i][j] = len[i][j-1];   
   
  }
 } 
 return len[n][m];
}

int main ()
{
 int len;
    int n,m,longer,shorter;
 while ((cin >> in_st >> in_ed)&&(in_st[0]!='0')&&(in_ed[0] !='0'))
 {
  n = strlen(in_st);
  m = strlen(in_ed);
  if (m > n) 
  {
   longer = m ;
   shorter = n;
  }
  else 
  {
   longer =  n;
   shorter = m;
  }
  len = shortest(n, m);
  int sign = 1 ;
  int temp = shorter;
  int sum = len ; 
  while ( sum * 2 < longer)
  {
   sum += shorter ;
   shorter *= 2 ;
      sign++;
  }
  cout<<sign<<endl; 
 }
 return 0;
}

posted on 2011-05-06 18:49  _Clarence  阅读(84)  评论(0编辑  收藏

导航

统计