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 dirtysalt at 2009-07-13 14:47:11 on Problem 3267
/* -*- c++ -*-
   copy[write] by dirlt(dirtysalt1987@gmail.com) */
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

char dict[610][32];
int W;
char mesg[310];
int L;
//DP[i]表示前i个字符串如果需要和dict匹配的话需要删除的字符。那么从i+1开始匹配匹配到j位置如果有n个删除的话,那么DP[j]=min(DP[j],DP[i]+n).
int DP[310];
int solve()
{
  for(int i=0;i<=L;i++)DP[i]=i;
  for(int i=1;i<=L;i++){
    int j=i-1;
    for(int k=0;k<W;k++){
      int lk=0,lj=j;
      int cnt=0;
      //从j开始进行匹配的话最长匹配到lj同时删除cnt个字符
      while(dict[k][lk]!=0 && mesg[lj]!=0){
	if(dict[k][lk]==mesg[lj]){
	  lk++;
	  lj++;
	}else{
	  cnt++;
	  lj++;
	}
      }
      if(dict[k][lk]==0){
	//一旦到某个字符删除字符数减少为n的话,那么后面的串就可能以n+1,n+2个这个字符数删除:-)
	//这也是为什么初始化DP[i]=i的原因.
	if((DP[j]+cnt)<DP[lj]){
	  DP[lj]=DP[j]+cnt;
	  for(int n=lj+1;n<=L;n++)
	    DP[n]=min(DP[n],DP[lj]+n-lj);
	}
      }
    }
  }
  return DP[L];
}

int main()
{
  scanf("%d %d",&W,&L);
  scanf("%s",mesg);
  for(int i=0;i<W;i++){
    scanf("%s",dict[i]);
  }
  printf("%d\n",solve());
  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