ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

The Cow Lexicon S

2022-06-06 14:04:09  阅读:142  来源: 互联网

标签:Lexicon Cow int d% len characters ans message


题目描述

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.

输入格式

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

输出格式

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.

样例 #1

样例输入 #1

6 10
browndcodw
cow
milk
white
black
brown
farmer

样例输出 #1

2

提示

感谢@ws_fuweidong 提供完整题面
首先考虑暴力。设计\(dp_i\)表示前i个字符要去掉多少个字符可以被翻译。暴力转移就是枚举上一位在j,在j+1i中只能有一个单词,那么枚举是找到j+1i中最长的那个单词。

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int w,l,dp[N],len[N];
char s[N][N],t[N];
int can(int x,int y)
{
	int ans=y-x+1;
	for(int k=1;k<=w;k++)//枚举选哪一种单词
	{
		int r=1;
		for(int i=x;i<=y;i++)
		{
			if(s[k][r]==t[i])
				++r;
			if(r>len[k])//可以匹配上
				ans=min(ans,y-x+1-len[k]);
		}
	}
	return ans;
}
int main()
{
	scanf("%d%d%s",&w,&l,t+1);
	for(int i=0;i<=l;i++)
		dp[i]=i;
	for(int i=1;i<=w;i++)
		scanf("%s",s[i]+1),len[i]=strlen(s[i]+1);
	for(int i=1;i<=l;i++)
		for(int j=0;j<i;j++)
			dp[i]=min(dp[i],dp[j]+can(j+1,i));
	printf("%d",dp[l]);
	return 0;
}

但是会超时获得70分。我们发现可以预处理出每个can(l,r)。枚举l和选哪种单词,然后往后推r,如果一个单词在某一处可以被匹配那么在后面都可以匹配的上。

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int w,l,dp[N],len[N],f[N][N];
char s[N][N],t[N];
int can(int x,int y)
{
	int ans=2e9;
	for(int k=1;k<=w;k++)
	{
		int r=1;
		for(int i=x;i<=y;i++)
		{
			if(s[k][r]==t[i])
				++r;
			if(r>len[k])
				ans=min(ans,y-x+1-len[k]);
		}
	}
	return ans;
}
int main()
{
	memset(f,0x7f,sizeof(f));
	scanf("%d%d%s",&w,&l,t+1);
	for(int i=0;i<=l;i++)
		dp[i]=i;
	for(int i=1;i<=w;i++)
		scanf("%s",s[i]+1),len[i]=strlen(s[i]+1);
//	return can(6,10);
	for(int i=1;i<=l;i++)
	{
		int ans=2e9;
		for(int k=1;k<=w;k++)
		{
			int r=1;
			for(int j=i;j<=l;j++)
			{
				if(s[k][r]==t[j])
					++r;
				if(r>len[k])
					f[i][j]=min(f[i][j],j-i+1-len[k]);
			}
		}
	}
	for(int i=1;i<=l;i++)
		for(int j=0;j<i;j++)
			dp[i]=min(dp[i],dp[j]+f[j+1][i]);
	printf("%d",dp[l]);
	return 0;
}

标签:Lexicon,Cow,int,d%,len,characters,ans,message
来源: https://www.cnblogs.com/mekoszc/p/16347954.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有