Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

真正自己过的DP,第一道,纪念~

Posted by lolihunter at 2009-09-16 07:03:40 on Problem 2250
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#define INF 100000
using namespace std;
int main()
{
  char str[32];
  string s1[105],s2[105],ans[105][105],s;
  int i,j,len1,len2,f[105][105];
  while(scanf("%s",str)!=EOF)
  {
    i=0;
    while(str[0]!='#')
    {
      i++;
      s1[i]=str;
      scanf("%s",str);                
    } 
    len1=i;
    i=0;
    scanf("%s",str);
    while(str[0]!='#')
    {
      i++;
      s2[i]=str;
      scanf("%s",str);                   
    }
    len2=i;
    memset(f,0,sizeof(f));
    f[0][0]=0;
    ans[0][0]="";
    for(i=1;i<=len1;i++)
    {
      f[i][0]=0;
      ans[i][0]="";                    
    }
    for(i=1;i<=len2;i++)
    {
       f[0][i]=0;
       ans[0][i]="";                   
    }
    for(i=1;i<=len1;i++)
    {
      for(j=1;j<=len2;j++)
      {
        if(s1[i]==s2[j])
        {
          if(f[i-1][j-1]+1>f[i][j])
          {
            f[i][j]=f[i-1][j-1]+1;
            ans[i][j]=ans[i-1][j-1]+s1[i]+" ";                         
          }                
        }
        if(f[i][j-1]>f[i][j])
        {
            f[i][j]=f[i][j-1];
            ans[i][j]=ans[i][j-1];                   
        }    
        if(f[i-1][j]>f[i][j])
        {
            f[i][j]=f[i-1][j];
            ans[i][j]=ans[i-1][j];                     
        }
      }                                      
    }
    int len=ans[len1][len2].size();
    s=ans[len1][len2];
    cout<<s.erase(len-1,1)<<endl;                     
  }
  return 0;    
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator