Poj 3267 the cow lexicon DP

Source: Internet
Author: User

Give some words and a sentence, and ask how many letters are removed from the sentence, which is completely composed of the given words.

DP [I] indicates the minimum number of letters to be removed from the first I digit of a sentence. The status transfer is as follows.

The last letter has two options: delete and retain, if Delete, then DP [I] = DP [I-1] + 1.

If not deleted, enumerate the words ending with str [I] to see if they can be inserted into the first I bit of STR and obtain the optimal value.

Code posted online =

# Include <iostream> # include <algorithm> # include <cmath> using namespace STD; int W, L, DP [305]; char STR [305], dict [601] [305]; int FN (INT pt) {int K, I, j, Mim, Len; MIM = 305; for (I = 0; I <W; I ++) {Len = strlen (dict [I]); If (STR [pt] = dict [I] [len-1] & Len <= Pt + 1) {// word ending with this character for (j = PT, K = len-1; j> = 0 & K> = 0; j --) if (STR [J] = dict [I] [k]) k --; If (k =-1) {If (j =-1) mim = min (MIM, Pt-J-len); // when STR is also searched, the length of else MIM = m is returned. In (MIM, DP [J] + Pt-J-len) ;}}// cout <MIM <Endl; return MIM;} int main () {int I; while (scanf ("% d", & W, & L )! = EOF) {memset (DP, 0, sizeof (DP); scanf ("% s", STR); for (I = 0; I <W; I ++) scanf ("% s", dict [I]); for (I = 0; I <L; I ++) {if (I = 0) DP [0] = min (1, FN (0); // separate, afraid that the word with a length of 1 appears in the dictionary. Else DP [I] = min (DP [I-1] + 1, FN (I);} printf ("% d \ n", DP [L-1]);} return 0 ;}
View code

 

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.