BZOJ1633: [Usaco2007 feb]the Cow Lexicon cow's Dictionary DP

Source: Internet
Author: User
Tags lowercase time limit
1633: [Usaco2007 feb]the Cow Lexicon cow's DictionaryTime Limit:5 Sec Memory limit:64 MB submit:582 solved:321 [submit][status][discuss] Description

Few people know that cows have their own dictionaries with W (1≤w≤600) words, each with a length of not more than 25, and consisting of lowercase letters. When they communicate, the words are always inaccurate for a variety of reasons. For example, Bessie heard someone say "Browndcodw" to her, The exact meaning is "browncow", more than two "D", these two "D" is probably the noise around. The cows find it hard to identify the strange information, so they want you to help identify a received message, a string containing only a lowercase letter and a length of L (2≤l≤300). Sometimes there are extra letters in this string, Your task is to find a minimum number of letters to make the string into an accurate "cow" (that is, an arrangement of some words in a dairy dictionary). Input

Line 1th: Two integers separated by a space, W and L.

Line 2nd: A string of length l that represents the information received. Line 3rd to w+2: Cow's dictionary, one word per line. Output

Unique line: An integer that means that at least a few letters are removed to make it an accurate "cow language".

Sample Input 6 10
Browndcodw
Cow
Milk
White
Black
Brown
Farmer

Sample Output 2
HINT

Source

Silver

[Submit] [Status] [Discuss]
F[i] Indicates the minimum number of characters to be deleted before overwriting the first I characters

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int n=1005;
int n,l,f[n];
Char a[n],c[n][35];
int main ()
{
	scanf ("%d%d", &n,&l);
	scanf ("%s", a+1);
	for (int i=1;i<=n;i++) scanf ("%s", c[i]+1);
	for (int i=1;i<=l;i++)
	{
		f[i]=i;
		for (int j=1;j<=n;j++)
		{
			int len=strlen (c[j]+1), cnt=0,k;
			for (k=i;k;k--)
			{
				if (c[j][len]==a[k]) len--;
				else cnt++;
				if (len==0) break;
			}
			if (len==0) f[i]=min (f[i],f[k-1]+cnt);
		}
	}
	printf ("%d", f[l]);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.