国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

POJ 3267 The Cow Lexicon (DP)

2019-11-11 02:11:13
字體:
供稿:網(wǎng)友

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(chǎn)’..’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(chǎn)’..’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 10browndcodwcowmilkwhiteblackbrownfarmer

Sample Output

2

題意

給出一個原始序列,以及w個單詞,問該序列至少需要去掉多少個字母之后,才能變成完全由已知單詞構(gòu)成的序列。

思路

從題目可以分析出,我們需要找到該序列對于所有單詞的最大不重合匹配數(shù),然后需要刪除的字母個數(shù)便是序列長度減去匹配數(shù)。

對于匹配這一動作,我們需要從前向后匹配單詞的字母,但是狀態(tài)數(shù)組中存儲的結(jié)果,如果也是從前往后更新的話會造成提前更新或者其他操作,因此我們對于序列采取從后向前遍歷的方法。

dp[i]代表序列[i,n-1]中最少刪除的字母個數(shù)。

首先對于遍歷到的每一位,先取最壞情況,此時 dp[i]=dp[i+1]+1

然后遍歷字典,找出所有首字母和序列當(dāng)前字母相同的單詞,然后進行匹配,若成功匹配該單詞后更新 dp[i]

其中: dp[i]=min(dp[i],dp[si]+si?i?len)

si 為匹配單詞結(jié)束后的序列游標(biāo),i為匹配前的序列游標(biāo),len為單詞長度

AC 代碼

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>using namespace std;#include<vector>#include<queue>char data[650][50],str[350];int w,l;int dp[350];int solve(){ dp[l]=0; for(int i=l-1; i>=0; i--) //從后向前遍歷 { dp[i]=dp[i+1]+1; //取最壞情況 for(int j=0; j<w; j++) //遍歷字典 { if(str[i]==data[j][0]) //第一位相同 { int si=i,di=0,len=strlen(data[j]); for(; si<l&&di<len; si++) //匹配字典中的單詞 if(str[si]==data[j][di]) di++; if(di==len) //匹配成功 dp[i]=min(dp[i],dp[si]+si-i-len); //更新dp } } } return dp[0];}int main(){ while(~scanf("%d%d%*c",&w,&l)) { gets(str); for(int i=0; i<w; i++) gets(data[i]);
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 无极县| 错那县| 新兴县| 五指山市| 巴里| 安多县| 沙雅县| 株洲县| 临西县| 林甸县| 南开区| 和顺县| 讷河市| 康保县| 府谷县| 泸水县| 邵阳市| 苏州市| 马边| 镇原县| 平潭县| 长治市| 丹江口市| 灌云县| 儋州市| 利津县| 双柏县| 阿荣旗| 徐闻县| 白水县| 西贡区| 陈巴尔虎旗| 阳朔县| 金塔县| 道孚县| 杨浦区| 周宁县| 含山县| 普兰店市| 东莞市| 莱西市|