您的位置:首页 > 其它

poj 3267 The Cow Lexicon

2012-10-14 20:53 453 查看
table[i]表示以i为结尾的字符串最优解

则有 table[i] = min(table[i-1]+1, table[j] + 删除字符数) j, j+1, j+2...i删掉字符后能够和词典中的单词匹配

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
//#pragma comment(linker, "/STACK:1024000000,1024000000") 

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;
using std::make_pair;
using std::greater;

const int INFI((INT_MAX-1) >> 1);

char str[610][30];
int len[610];
char s[310];
int table[610];

int main()
{
	int w, l;
	while(~scanf("%d%d", &w, &l))
	{
		scanf("%s", s+1);
		for(int i = 1; i <= w; ++i)
		{
			scanf("%s", str[i]);
			len[i] = strlen(str[i]);
		}
		table[0] = 0;
		for(int i = 1; i <= l; ++i)
		{
			table[i] = INFI;
			for(int j = 1; j <= w; ++j)
				if(len[j] <= i && s[i] == str[j][len[j]-1])
				{
					int p1 = i, p2 = len[j]-1;
					int count = 0;
					while(p1 >= 1 && p2 >= 0)
						if(s[p1] == str[j][p2])
						{
							--p1;
							--p2;
						}
						else
						{
							--p1;
							++count;
						}
					if(p2 == -1)
						table[i] = min(table[i], table[p1]+count);
				}
			table[i] = min(table[i], table[i-1]+1);
		}
		printf("%d\n", table[l]);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: