【BZOJ】3297: [USACO2011 Open]forgot(dp)

http://www.lydsy.com/JudgeOnline/problem.php?id=3297

这题拖了很久呢。。。

很久以前写了个dfs,,但是tle了。。。。。

然后一直想dp想不出来,写出来了又wa了。。

然后看到一英文题解,暂时就只会这样了。。

其实dp方程很简单。。。。。。

f[i]=min(f[i], f[j]+a[x]),f[i]是个字符串,所以用cpp自带的string处理字典序。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i] << ' '; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=1005;
int n, L, can[N][N], len[N];
string s[N], d[N];
char a[N], tp[N];
bool check(int x, int l) {
	int sz=len[x];
	if(l+sz>L) return 0;
	rep(i, sz) if(a[l+i]!='?' && a[l+i]!=s[x][i]) return 0;
	return 1;
}
int main() {
	read(L); read(n);
	scanf("%s", a);
	for1(i, 1, n) scanf("%s", tp), s[i]=tp, len[i]=strlen(tp);
	for1(i, 1, n) rep(j, L) can[i][j]=check(i, j);
	for3(k, L-1, 0)
		for1(i, 1, n) {
			int j=k+len[i];
			if(can[i][k] && (j==L || d[j]!="") && (d[k]=="" || d[k]>s[i]+d[j])) 
					d[k]=s[i]+d[j];
		}
	printf("%s", d[0].c_str());
	return 0;
}

 

 


 

 

Description

   发生了这么多,贝茜已经忘记了她cowtube密码。然而,她记得一些有用的信息。
首先,她记得她的密码(记为变量P)长度为L(1 <= L<=1,000)字符串,并可以被分成
一个或多个词(不一定是唯一的),词来自于字典中NW(1<=NW<=1,000)个独特的词。
一个词W_i,被定义为一个长度1..20的小写字母序列('a'..'z')。
她还记得她密码中某些字母的位置。
请看下面的例子。贝西知道她的密码看起来像"a??l?ban???????"('?'代表一个字母,她不记得),
她的字典里有下面的词:

apple
cow
farmer
banana
bananas
pies

贝西有两个可能的密码是的“applebananapies”和“applebananascow”。
给你字典,贝西记得的字母,请找到她的密码。如果有一个以上的密码是可能的,找到字典序最前的。

Input

*第1行:两个空格分隔的整数:L和NW
*第2行:一个字符串,长度为L:P
*第3..N+2W行:第I+2行包含在字典中的第i个字:W_i

Output

 

*第1行:密码

Sample Input

15 6
a??l?ban???????
apple
cow
farmer
banana
bananas
pies

Sample Output

applebananapies

HINT

Source

posted @ 2014-09-25 12:39  iwtwiioi  阅读(218)  评论(1编辑  收藏  举报