The Cow Lexicon

本文介绍了一种算法,旨在帮助解析牛群的通信消息,通过最小化额外字符的删除来重构准确的字典单词序列。面对接收的混乱信息,该算法运用动态规划策略,遍历字典中的所有单词,确定需要删除的最少字符数量,以恢复原始的通信内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 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 10 browndcodw cow milk white black brown farmer
 
Sample Output
2
******************************************************************************************************
本题的状态转移方程 dp[i]=min(dp[j]+seg);即i为末尾,j为枚举单词表中的单词数某个单词到目标句子中的j个单词结束所删最小
******************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 int dp[1001];
 9 int i,n,m;
10 char str[605][403];
11 char po[500];
12 int min(int a,int b)
13  {
14      return a>b?b:a;
15  }
16 int  find(int end)//找以i为终点的的最优解
17  {
18      int min2=1005;
19      int len;
20      int j,k;
21      for(int h=1;h<=n;h++)
22       {
23           len=strlen(str[h]);
24            if(str[h][len-1]==po[end]&&end>=len-1)
25            {
26                 for(j=len-1,k=end;j>=0&&k>=0;k--)
27                   if(str[h][j]==po[k])j--;
28                 if(j==-1)
29                 {
30                  if(k==-1)
31                   min2=min(min2,end-k-len);
32                  else
33                   min2=min(min2,dp[k]-k-len+end);
34                 }
35            }
36 
37 
38       }
39     //cout<<min2<<endl;
40      return min2;
41  }
42 
43  int main()
44  {
45      cin>>n>>m;
46      memset(dp,0,sizeof(dp));
47      scanf("%s",po);
48      getchar();
49      for(i=1;i<=n;i++)
50       scanf("%s",str[i]);
51      for(i=0;i<m;i++)
52       {
53           if(i==0)
54             dp[0]=min(1,find(0));
55            else
56             dp[i]=min(dp[i-1]+1,find(i));
57       }
58     cout<<dp[m-1]<<endl;
59     return 0;
60  }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3247491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值