hdu 1159

OJ

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define PB(x)(scanf("%I64d",&x))
#define PRB(x)(printf("%I64d\n",(x)))
typedef __int64 LL;
#define N 100005
#define M 1005
#define Mod 1000
#define Inf 0x7fffffff

string a,b;
int dp[M][M];

int ma(int a,int b)
{
	return a>b?a:b;
}
int maxx(int a,int b,int c)
{
	return ma(ma(a,b),c);
}
void init()
{
  while(cin>>a>>b)
  {
	  memset(dp,0,sizeof(dp));
	  int len = a.length();
	  int l = b.length();
	  for(int i=0;i<len;i++)
		  for(int j=0;j<l;j++)
		  {
			 if(i==0 && j==0)
				 if(a[i] == b[j]) {dp[i][j]=1;continue;}
			 if(a[i] == b[j]&&(i==0||j==0))
			 {
				 if(i==0) dp[i][j]=1;
				 if(j==0) dp[i][j]=1;
				 continue;
			 }
			 if(i==0) dp[i][j] = dp[i][j-1];
			 else if(j==0) dp[i][j] = dp[i-1][j];
			 else 
			 {
				 if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;
				 dp[i][j] = maxx(dp[i][j],dp[i-1][j],dp[i][j-1]);
			 }

		  }
		  cout<<dp[len-1][l-1]<<endl;
  }
return ;
}

int main()
{
init();
return 0;
}


你可能感兴趣的:(c)