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

首頁 > 學院 > 開發設計 > 正文

POJ 3267 The Cow Lexicon (DP)

2019-11-11 00:52:58
字體:
來源:轉載
供稿:網友

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 10browndcodwcowmilkwhiteblackbrownfarmer

Sample Output

2

題意

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

思路

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

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

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

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

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

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

si 為匹配單詞結束后的序列游標,i為匹配前的序列游標,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]);
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潢川县| 绥滨县| 河池市| 东乡| 合作市| 慈溪市| 庆阳市| 临邑县| 竹溪县| 安图县| 当雄县| 平安县| 桑植县| 理塘县| 永胜县| 加查县| 二手房| 新昌县| 鄯善县| 淮阳县| 鸡泽县| 贞丰县| 皮山县| 怀仁县| 禹城市| 若羌县| 阿巴嘎旗| 铜山县| 灯塔市| 天水市| 汝城县| 新龙县| 比如县| 富顺县| 尼勒克县| 琼中| 绥滨县| 鄂州市| 黔江区| 油尖旺区| 大丰市|