HDOJ-三部曲-1015-The Cow Lexicon

原文链接: http://www.cnblogs.com/aljxy/p/3315660.html

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;
}
 
 

转载于:https://www.cnblogs.com/aljxy/p/3315660.html

猜你喜欢

转载自blog.csdn.net/weixin_30338497/article/details/95160080