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,1ac 经典DP

Posted by lolihunter at 2009-09-17 22:45:12 on Problem 3356
#include<stdio.h>
  int len1,len2,f[1005][1005],min;
  char s1[1005],s2[1005];
int main()
{

  while(scanf("%d",&len1)!=EOF)
  {
    scanf("%s",s1+1);
    scanf("%d %s",&len2,s2+1);
    for(int i=1;i<=len1;i++)
    {
      f[i][0]=i;        
    }
    for(int i=1;i<=len2;i++)
    {
      f[0][i]=i;        
    }
    f[0][0]=0;
    for(int i=1;i<=len1;i++)
    {
      for(int j=1;j<=len2;j++)
      {
         min=99999;
         if(s1[i]==s2[j])
         {
           if(f[i-1][j-1]<min)
             min=f[i-1][j-1];                
         }
         else
         {
           if(f[i-1][j-1]+1<min)
             min=f[i-1][j-1]+1;
           if(f[i-1][j]+1<min)
             min=f[i-1][j]+1;
           if(f[i][j-1]+1<min)
             min=f[i][j-1]+1;
         }    
         f[i][j]=min;   
      }        
    } 
    printf("%d\n",f[len1][len2]);                           
  }  
    
  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