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

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

POJ 3267 The Cow Lexicon (DP)

2019-11-10 23:55:24
字體:
供稿:網(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

題意

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

思路

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

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

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

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

然后遍歷字典,找出所有首字母和序列當(dāng)前字母相同的單詞,然后進(jìn)行匹配,若成功匹配該單詞后更新 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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 莱州市| 瑞金市| 视频| 河源市| 苍南县| 东城区| 迭部县| 揭东县| 万载县| 洛浦县| 女性| 临洮县| 大名县| 孝义市| 旬阳县| 手游| 大姚县| 开远市| 镇远县| 平塘县| 凤台县| 依兰县| 贵南县| 海门市| 迭部县| 虎林市| 廉江市| 舟山市| 遂平县| 临潭县| 和林格尔县| 巩义市| 永寿县| 嘉义县| 应用必备| 毕节市| 明星| 蒙山县| 拉萨市| 辉南县| 资中县|