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-7      ;

const int M = 660000         ;

const ll P = 10000000097ll   ;

const int INF = 0x3f3f3f3f   ;

const int MAX_N = 20         ;

const int MAXSIZE = 101000000;



string str[220];

char a[2200000];

bool vis[220000];

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(".") == 0)  break;

    }

    while(fin >> a[len_a++])

    vis[0] = true;

    for(i = 0; i < len_a; ++i){

        if(!vis[i]) continue;

        for(j = 1; j < str_num; ++j){

            int length_str = str[j].length();

            if(length_str + i >= len_a)   continue;

            for(k = 0; 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 0;

}

 

你可能感兴趣的:(USACO)