zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    《ML in Action》笔记(2) —— ID3决策树
    《ML in Action》笔记(1) —— kNN分类器
    MYSQL笔记
    Javascript代码摘录
    初试mysql存储过程&触发器
    百度地图API应用实践(一) —— 栅格图(草稿)
    2020年8月9日, 网吧, 歌单, 极客时间, 龙岩网络图书馆, 正则, WPS, Python
    2020年8月3日, 网吧 ,
    2020年7月13日,想在网吧搞学习,实属想多了
    账号被盗,什么原因呢?是我的操作系统太脆弱,还是博客园存在安全隐患?
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4296969.html
Copyright © 2011-2022 走看看