/*
ID:kevin_s1
PROG:prefix
LANG:C++
*/ #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath> using namespace std; #define sigma_size 26
#define MAX_LEN 2000001 //gobal variable==== struct trie_node{
trie_node* next[sigma_size];
bool is_terminal;
trie_node(){
memset(next, 0, sizeof(next));
is_terminal = false;
}
}; struct Trie{
trie_node* root;
int size;
int idx(char ch){
return ch - 'A';
}
Trie(){
root = new trie_node();
size = 1;
} void Insert(string str){
trie_node* cur = root;
int len = str.length();
for(int i = 0; i < len; i++){
int ch = idx(str[i]);
if(cur->next[ch] == NULL){
cur->next[ch] = new trie_node();
}
cur = cur->next[ch];
}
cur->is_terminal = true;
} bool Query(string str){
trie_node* cur = root;
int len = str.length();
for(int i = 0; i < len; i++){
int ch = idx(str[i]);
if(cur->next[ch] == NULL){
return false;
}
else
cur = cur->next[ch];
} if(cur->is_terminal)
return true;
else
return false; } }; int DP[MAX_LEN];
string S;
//================== //function========== //================== int main(){
freopen("prefix.in","r",stdin);
freopen("prefix.out","w",stdout);
Trie prefix;
string str;
char ch[201];
while(scanf("%s",ch) && strcmp(ch, ".") != 0){
str = ch;
prefix.Insert(str);
}
str.clear();
int c;
while((c = getchar()) != EOF){
if(!isspace(c))
S = S + (char)c;
}
int Len = S.length();
memset(DP, 0, sizeof(DP));
DP[Len] = 0;
for(int i = Len - 1; i >= 0; --i){
for(int j = 1; j <= 10 && (i + j - 1) < Len; ++j){
string sub = S.substr(i, j);
if(prefix.Query(sub)){
if(DP[i + j] + j > DP[j])
DP[i] = DP[i + j] + j;
}
}
}
cout<<DP[0]<<endl;
return 0;
}

USACO prefix TrieTree + DP的更多相关文章

  1. USACO Money Systems Dp 01背包

    一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...

  2. USACO奶牛博览会(DP)

    Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...

  3. [USACO]奶牛博览会(DP)

    Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...

  4. USACO 状压DP练习[3]

    1725 题意:$m*n:\ m,n \le 12$的牧场,有的格子不能选,相邻不能同时选,求方案数 $f[i][j]$前$i$行当前行选的集合为$j$ #include <iostream&g ...

  5. Codeforces 643C Levels and Regions 斜率优化dp

    Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...

  6. 模板 - 动态规划 - 区间dp

    因为昨天在Codeforces上设计的区间dp错了(错过了上紫的机会),觉得很难受.看看学长好像也有学,就不用看别的神犇的了. 区间dp处理环的时候可以把序列延长一倍. 下面是 $O(n^3)$ 的朴 ...

  7. Uvalive-4494-(数位dp)

    题意:求a->b中的二进制出现过多少个1,很显然的数位dp,对于某一位来说,如果这位是0,那么dp[i]=dp[i-1]  如果这一位是1 那么dp[i]=dp[i-1]+1<<(p ...

  8. 牛客国庆集训派对Day3 Solution

    A    Knight 留坑. B    Tree 思路:两次树形DP,但是要考虑0没有逆元 可以用前缀后缀做 #include <bits/stdc++.h> using namespa ...

  9. P1474货币系统

    这是USACO的一道DP题,难度是提高—. 这道题是告诉我们货币种类,问你用这些货币组成一个面值最大有多少种方案.第一眼看上去想用dfs记忆化,随后发现其实这个题很类似于完全背包,可以取无线件,但是他 ...

随机推荐

  1. bjfu1099 度度熊大战僵尸

    这也是2011年百度之星的一道题. 这题我就是乱搞搞过的,打代码之前自己心里也没底,不知道能不能过的. 我的做法很简单,就是按时间顺序依次构造能杀死的僵尸血量,找到第k小的.构造的方法也很暴力:对t时 ...

  2. delegate 为什么用 weak属性

    weak指针主要用于“父-子”关系,父亲拥有一个儿子的strong指针,因此是儿子的所有者:但是为了阻止所有权回环,儿子需要使用weak指针指向父亲:你的viewcontroller通过strong指 ...

  3. bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...

  4. 一起刷LeetCode1-Two Sum

    感觉有必要重新刷刷题了,为以后找工作做做准备,选择LeetCode+topcoder上的Data Science Tutorials, 争取每天晚上10:00开始刷一道,复习一下相关知识点. ---- ...

  5. <Chapter 2>2-2-2.开发Java应用(Developing a Java App)

    App Engine的Java网络应用使用了Java Servlet标准接口来和应用服务器交互.一个应用由一个或多个servlet类组成,每个都扩展了(extend)servlet基类.使用一个叫做部 ...

  6. [转]python起步之卡尔曼滤波

    原文地址:http://www.niwozhi.net/demo_c65_i50946.html 关于卡尔曼滤波的理论这里不打算讲了,就是那个5个基本的公式,这里直接给出公式: 公式1:X(k|k-1 ...

  7. HDU5806:NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接:HDU5806 题意:找出有多少个区间中第k大数不小于m. 分析:用尺取法可以搞定,CF以前有一道类似的题目. #include<cstdio> using namespace ...

  8. 在Dashboard中显示课表/日程表

    对于使用Mac系统的朋友们来说,Dashboard一定并不陌生.通过Dashboard我们可以方便地添加小组件,查看日历,天气,便签等等.然而,这些都是“定制”的内容.如何在Dashboard中显示自 ...

  9. Spring Auto-Wiring Beans with @Autowired annotation

    In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...

  10. Spring @PostConstruct and @PreDestroy example

    In Spring, you can either implements InitializingBean and DisposableBean interface or specify the in ...