USACO prefix TrieTree + DP
/*
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的更多相关文章
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- USACO奶牛博览会(DP)
Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...
- [USACO]奶牛博览会(DP)
Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...
- USACO 状压DP练习[3]
1725 题意:$m*n:\ m,n \le 12$的牧场,有的格子不能选,相邻不能同时选,求方案数 $f[i][j]$前$i$行当前行选的集合为$j$ #include <iostream&g ...
- Codeforces 643C Levels and Regions 斜率优化dp
Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...
- 模板 - 动态规划 - 区间dp
因为昨天在Codeforces上设计的区间dp错了(错过了上紫的机会),觉得很难受.看看学长好像也有学,就不用看别的神犇的了. 区间dp处理环的时候可以把序列延长一倍. 下面是 $O(n^3)$ 的朴 ...
- Uvalive-4494-(数位dp)
题意:求a->b中的二进制出现过多少个1,很显然的数位dp,对于某一位来说,如果这位是0,那么dp[i]=dp[i-1] 如果这一位是1 那么dp[i]=dp[i-1]+1<<(p ...
- 牛客国庆集训派对Day3 Solution
A Knight 留坑. B Tree 思路:两次树形DP,但是要考虑0没有逆元 可以用前缀后缀做 #include <bits/stdc++.h> using namespa ...
- P1474货币系统
这是USACO的一道DP题,难度是提高—. 这道题是告诉我们货币种类,问你用这些货币组成一个面值最大有多少种方案.第一眼看上去想用dfs记忆化,随后发现其实这个题很类似于完全背包,可以取无线件,但是他 ...
随机推荐
- Android 嵌套GridView,ListView只显示一行的解决办法
重写ListView.GridView即可: public class MyListView extends ListView { public MyListView(Context context) ...
- java移动/赋值文件 copy/move file
public class FileAccess { public static boolean Move(File srcFile, String destPath) { // Destination ...
- Slalom
题意: 有n个宽度为w的门,给出门的左端点的水平位置x和高度y,和恒定的垂直速度,现有s个速度,求能通过这n个门的最大速度. 分析: 二分速度判断 #include <map> #incl ...
- 在PC上测试移动端网站和模拟手机浏览器的5大方法
在PC上测试移动端网站和模拟手机浏览器的5大方法 来源:互联网 作者:佚名 时间:03-19 10:14:54 [大 中 小] 最近公司要开发网站的移动版,让我准备准备知 ...
- SQL你必须知道的-查询聚合分组排序
use MySchoolTwo -- 简单查询 select * from Student -- 话说这种查询的效率要比 * 要高级点 select sId , sName , ...
- 在ubuntu下安装chrome
To add PPA in Ubuntu 14.04 / 13.10 / 13.04 / 12.10 / 12.04 First download and install the key from G ...
- Java中的return
比如你写了一个叫getInt的类public int getInt(){ //这个类的意思就是一个具有返回值类型为int的类了 //通常如果不需要返回值的话 这里就写void....//你的具体代码r ...
- android判断当前网络状态及跳转到设置界面
今天,想做这个跳转到网络设置界面, 刚开始用 intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 不料老是出现settings.Wirele ...
- windows7+eclipse-jee-luna+hadoop2.6运行环境及eclipse plugin插件编译
一.hadoop集群环境配置 参见:<Hadoop2.6集群环境搭建(HDFS HA+YARN)原来4G内存也能任性一次.> Win7环境: 登录用户名:hadoop , 与Hadoop ...
- nodejs+express +jade模板引擎 新建项目
先 安装 nodejsiDEAAM 2015/7/16 22:47:25然后安装 npm install expressiDEAAM 2015/7/16 22:47:35然后安装 npm instal ...