Poj3267 (the cow lexicon)

Source: Internet
Author: User

Address: the cow lexicon

 

Question:

The cow has its own language for word recognition. It has its own dictionary sequence. If a string of characters does not match the words in the dictionary, the cow cannot recognize them, your task is to find out the words in the given string that contain the dictionary of cows, and delete at least a few characters from the main string so that the main string only contains the words in the dictionary of cows, it does not contain more than characters.

 

Solution:

Dynamically plan the DP. DP has never been quite familiar with it. without reading the problem-solving report, the state equation cannot be pushed out, And the entry level is not enough. You need to study this knowledge point well.

 

State equation: DP [I] I represents the minimum number of characters to be deleted from I to L.

There are two ideas:

First, it cannot be matched successfully: DP [I] = DP [I + 1] + 1; (push forward from the back)

Second, the matching is successful: DP [I] = min (DP [I], DP [P1 + 1] + p1 + 1-i-len );

Explain the second statement:

DP [P1 + 1] + p1 + 1-i-len. Because the matching is successful, the string array I starts to match. P1 indicates that the matching with the dictionary string ends at the position of the Main string. P1 + 1-I indicates how many characters are used in the master string to match the dictionary string. Then-len indicates the number of characters to be deleted, and + dp [P1 + 1] indicates the minimum number of characters to be deleted from position P1 + 1 to position L. Therefore, the entire formula represents the minimum number of items deleted when the DP [I] match is successful. Because it may match the cow dictionary string many times from the master string I position to the P1 position, we should select a smaller DP [I].

 

Code:

1 # include <algorithm> 2 # include <iostream> 3 # include <sstream> 4 # include <cstdlib> 5 # include <cstring> 6 # include <cstdio> 7 # include <string> 8 # include <bitset> 9 # include <vector> 10 # include <queue> 11 # include <stack> 12 # include <cmath> 13 # include <list> 14 # include <map> 15 # include <set> 16 using namespace STD; 17 /************************************** */18 # define ll long long19 # define int6 4 _ int6420 # define PI 3.141592721 /******************************* * ******/22 const int INF = 0x7f7f7f7f; 23 const double EPS = 1e-8; 24 const double PIE = ACOs (-1.0); 25 const int d1x [] = {0,-, 1 }; 26 const int d1y [] = {-,}; 27 const int d2x [] = {0,-, 1}; 28 const int d2y [] =, -}; 29 const int FX [] = {-1,-1,-,}; 30 const int FY [] = {-, 1, -,-, 1}; 31/* vector <int> map [N]; Map [A]. P Ush_back (B); int Len = map [v]. size (); */32 /************************************ * **/33 void openfile () 34 {35 freopen ("data. in "," rb ", stdin); 36 freopen (" data. out "," WB ", stdout); 37} 38 priority_queue <int> qi1; 39 priority_queue <int, vector <int>, greater <int> qi2; 40/*********************** Lili split line, the above is the template section ****************/41 int min (int A, int B) 42 {43 if (a <B) 44 return a; 45 return B; 46} 47 int main () 48 {49 Int n, m; 50 while (scanf ("% d", & M, & N )! = EOF) 51 {52 char S1 [305], S2 [605] [305]; 53 int DP [305]; 54 memset (S1, 0, sizeof (S1 )); 55 memset (S2, 0, sizeof (S2); 56 memset (DP, 0, sizeof (DP); 57 int I, j; 58 scanf ("% s ", s1); 59 for (I = 0; I <m; I ++) 60 scanf ("% s", S2 [I]); 61 int len1 = strlen (S1); 62 DP [len1] = 0; 63 for (I = len1-1; I> = 0; I --) 64 {65 DP [I] = DP [I + 1] + 1; 66 for (j = 0; j <m; j ++) 67 {68 int len2 = strlen (s2 [J]); 69 int p1 = I; 70 int P2 = 0; 71 while (len1-i> = len2 & S1 [I] = S2 [J] [0]) 72 {73 If (S1 [P1 ++] = S2 [J] [P2]) 74 {75 P2 ++; 76} 77 If (P2 = len2) 78 {79 DP [I] = min (DP [I], DP [P1] + p1-i-len2); // There is no P1 + 1 here, reason: P1 ++; 80 break; 81} 82 If (P1> = len1) 83 break; 84 85} 86} 87} 88 printf ("% d \ n", DP [0]); 89} 90 return 0; 91}
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.