首页 > 代码库 > Gym 100962J Jimi Hendrix (树形DP)

Gym 100962J Jimi Hendrix (树形DP)

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

析:树形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)