USACO Longest Prefix 【水】

用Dp的思想解决了这道题目,也就是所谓的暴力= =

题意:给出一个集合,一个字符串,找出这个字符串的最长前缀,使得前缀可以划分为这个集合中的元素(集合中的元素可以不全部使用)。

还不会Trie 树QAQ

Source Code:

/*
ID: wushuai2
PROG: prefix
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)
#define RV(num) ((num) > 0 ? 0 : 1) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; string str[];
char a[];
bool vis[];
int str_num, len, len_a; int main() {
ofstream fout ("prefix.out");
ifstream fin ("prefix.in");
int i, j, k, l, m, n, t, s, c, w, q, num;
for(;;){
fin >> str[++str_num];
if(str[str_num].compare(".") == ) break;
}
while(fin >> a[len_a++])
vis[] = true;
for(i = ; i < len_a; ++i){
if(!vis[i]) continue;
for(j = ; j < str_num; ++j){
int length_str = str[j].length();
if(length_str + i >= len_a) continue;
for(k = ; k < length_str; ++k){
if(a[i + k] != str[j][k]) break;
}
if(k == length_str){
checkmax(len, length_str + i);
vis[length_str + i] = true;
}
}
}
fout << len << endl; fin.close();
fout.close();
return ;
}
上一篇:使用PHP处理POST上传时$_FILES数组为何为空


下一篇:HDU1034 Candy Sharing Game