题意:给定一棵树,然后每条边有一个字母,然后给定一行字符串,问你能不能从这棵树上找到,并输出两个端点。

析:树形DP,先进行递归到叶子结点,然后再回溯,在回溯的时候要四个值,一个是正着匹配的长度和端点,一个是反着匹配的长度和端点,

然后一个一个匹配,并不断更新这个长度和端点。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define print(a) printf("%d\n", (a))
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<char> w[maxn];
vector<int> G[maxn];
int ansx, ansy;
struct Node{
int lans, rans;
int l, r;
};
Node dp[maxn];
char s[maxn]; bool dfs(int u, int fa){
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(v == fa) continue;
if(dfs(v, u)) return true; int l = s[dp[v].l+1] == w[u][i] ? dp[v].l+1 : dp[v].l;
int r = s[m-dp[v].r] == w[u][i] ? dp[v].r+1 : dp[v].r;
if(l + dp[u].r >= m){
ansx = dp[v].lans;
ansy = dp[u].rans;
return true;
}
else if(r + dp[u].l >= m){
ansx = dp[u].lans;
ansy = dp[v].rans;
return true;
}
if(l > dp[u].l){
dp[u].l = l;
dp[u].lans = dp[v].lans;
}
if(r > dp[u].r){
dp[u].r = r;
dp[u].rans = dp[v].rans;
}
}
return false;
} int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i) G[i].clear(), w[i].clear();
int u, v;
char ch;
for(int i = 1; i < n; ++i){
scanf("%d %d %c", &u, &v, &ch);
dp[i].l = dp[i].r = 0;
dp[i].lans = dp[i].rans = i;
w[u].push_back(ch);
w[v].push_back(ch);
G[u].push_back(v);
G[v].push_back(u);
}
dp[n].l = dp[n].r = 0;
dp[n].lans = dp[n].rans = n; scanf("%s", s+1);
ansx = ansy = -1;
dfs(1, -1);
printf("%d %d\n", ansx, ansy);
}
return 0;
}

Gym 100962J Jimi Hendrix (树形DP)的更多相关文章

  1. 树形DP Gym 100496H House of Representatives

    题目传送门 /* 题意:寻找一个根节点,求min f(u) = ∑ρ(v, u) * p(v).ρ(v, u)是u到v的距离,p(v)是v点的权值 树形DP:先从1出发遍历第一次,sum[u]计算u到 ...

  2. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  3. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  4. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  5. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  6. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  7. BZOJ 2286 消耗战 (虚树+树形DP)

    给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...

  8. POJ2342 树形dp

    原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...

  9. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

随机推荐

  1. 【POJ1185】炮兵阵地(状压DP)

    题意: 思路:状压DP经典题 可以预处理下每一行内合法的状态,发现很少 所以转移时可以使用状态的编号而不是状态本身 DP时记录前两行状态的编号进行转移和判断 #include<cstdio> ...

  2. visual svn 搭建

    详细出处参考:http://www.jb51.net/article/17365.htm 这里提示一个需要注意的地方: 在签入源代码到SVN服务器的时候: 点击Import,弹出下面的窗体(图2-2- ...

  3. excludepathpatterns 无效

    踩坑了,调了好久才调出来. 原因:  访问的API /XXX 已经转换为 /error 了.  把“/error” 也加入 excludepathpatterns 里面即可.

  4. python学习之-- assert断言

    assert 断言作用:断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假.可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常.举例如下:a ...

  5. JAVA实现选择排序,插入排序,冒泡排序,以及两个有序数组的合并

    一直到大四才开始写自己的第一篇博客,说来实在有点羞愧.今天写了关于排序的算法题,有插入排序,冒泡排序,选择排序,以下贴上用JAVA实现的代码: public class test5 { public ...

  6. Linux下设置swappiness参数来配置内存使用到多少才开始使用swap分区(转)

    swappiness的值的大小对如何使用swap分区是有着很大的联系的.swappiness=0的时候表示最大限度使用物理内存,然后才是swap空间,swappiness=100的时候表示积极的使用s ...

  7. MySQL查询count(*)、count(1)、count(field)的区别收集

    经过查询研究得出这个和MySQL中用什么引擎有关,比如InnoDB和MyISAM在处理这count(*).count(1).count(field)都有不同的方式,还有就是和版本都有关系,不同的版本会 ...

  8. Mybatis各种模糊查询(转)

    模糊查询: 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('% ...

  9. Broadcom的消息机制

    在Broadcom中提供了自己的消息机制,有两种消息形式:Request/Response and Event(事件) Request/Response消息:进程之间的通信都是通过smd,所有的消息都 ...

  10. Capture and report JavaScript errors with window.onerror

    原文:https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html onerror is a sp ...