zoukankan      html  css  js  c++  java
  • HDOJ-三部曲-1015-The Cow Lexicon

    The Cow Lexicon

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 16   Accepted Submission(s) : 10
    Problem Description

    Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'..'z'. Their cowmunication system, based on mooing, is not very accurate; sometimes they hear words that do not make any sense. For instance, Bessie once received a message that said "browndcodw". As it turns out, the intended message was "browncow" and the two letter "d"s were noise from other parts of the barnyard.

    The cows want you to help them decipher a received message (also containing only characters in the range 'a'..'z') of length L (2 ≤ L ≤ 300) characters that is a bit garbled. In particular, they know that the message has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.

     
    Input
    Line 1: Two space-separated integers, respectively: W and L Line 2: L characters (followed by a newline, of course): the received message Lines 3..W+2: The cows' dictionary, one word per line
     
    Output
    Line 1: a single integer that is the smallest number of characters that need to be removed to make the message a sequence of dictionary words.
     
    Sample Input
    6 10 browndcodw cow milk white black brown farmer
     
    Sample Output
    2
     
    Source
    PKU
     
     
    自己的方法把所有的从网上找的测试数据都过了,就是一直WA...最后没办法还是用了别人的方法,看懂了以后自己又凭自己理解敲了一遍,和原作者的代码差不多。。。。。。
     
    状态转移方程dp[i]=min(dp[i+1]+1,dp[i1]+i1-i-len),dp[i]的意思是从第i个到第L个字符需要删除的最少的字符数
    #include<iostream>
    #include<string.h>
    using namespace std;
    int main()
    {
    	int W,L,dp[301],i,j;
    	char word[301],dic[601][301];
    	cin>>W>>L>>word;
    	for(i=0;i<W;i++)
    		cin>>dic[i];
    	dp[L]=0;
    	for(i=L-1;i>=0;i--)
    	{
    		dp[i]=dp[i+1]+1;
    		for(j=0;j<W;j++)         
    		{
    			int len=strlen(dic[j]);
    			if(L-i>=len&&word[i]==dic[j][0])   
    			{
    				int i1=i,i2=0;
    				while(i1<L)
    				{
    					if(dic[j][i2]==word[i1++])
    						i2++;
    					if(i2==len)
    					{
    						dp[i]=min(dp[i],dp[i1]+i1-i-len);
    						break;
    					}
    				}
    			}
    		}
    
    	}
    	cout<<dp[0]<<endl;
    }
    
     
     
  • 相关阅读:
    Spring IoC容器初的初始化过程
    IoC容器初始化过程
    花了一个星期,我终于把RPC框架整明白了!
    RPC简介及框架选择
    ORM框架的前世今生
    什么是索引?Mysql目前主要的几种索引类型
    面试官:谈谈你对Mysql的MVCC的理解?
    sql server 获取数据字段(表的字段和类型信息)
    YAML
    Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
  • 原文地址:https://www.cnblogs.com/aljxy/p/3315660.html
Copyright © 2011-2022 走看看