Antonidas

Time Limit: 4000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 5469
64-bit integer IO format: %I64d      Java class name: Main

 

Given a tree with N vertices and N−1 edges. Each vertex has a single letter Ci. Given a string S, you are to choose two vertices A and B, and make sure the letters catenated on the shortest path from A to B is exactly S. Now, would you mind telling me whether the path exists?

Input
The first line is an integer T, the number of test cases.
For each case, the first line is an integer N. Following N−1 lines contains two integers a and b, meaning there is an edge connect vertex a and vertex b.
Next line contains a string C, the length of C is exactly N. String C represents the letter on each vertex.
Next line contains a string S.
$1\leq T\leq 200, 1\leq N\leq 10^4, 1\leq a,b\leq N, a\not = b, |C|=N, 1\leq |S|\leq 10^4$. String C and S both only contain lower case letters.

Output
First, please output "Case #k: ", k is the number of test case. See sample output for more detail.
If the path exists, please output “Find”. Otherwise, please output “Impossible”.

Sample Input

2

7
1 2
2 3
2 4
1 5
5 6
6 7
abcdefg
dbaefg 5
1 2
2 3
2 4
4 5
abcxy
yxbac

Sample Output

Case #1: Find
Case #2: Impossible

Source

 
解题:点分治+hash
 debug大半天的成果啊
比如一个目标串是$abcdefg$,假设我们树分治的时候,选的根恰好是$d$,那么我们需要的怎样的hash信息?是不是从$d->a$以及$d->g$的hash信息啊,注意方向。d才是高位!!!
所以我们要预处理出来目标串每个字符么,向左走到端点已经向右走到端点的hash值。
在树分治的时候,我们可以从根到当前点,走出一条路,路上的点就会hash出一个值,根据深度,我们可以看看它跟这个长度的前缀配不配,或者跟这个长度的后缀配不配,配的话,我们要分别找剩下长度的后缀或者前缀是否存在。
比放说根就是$d$,当前走到深度4,那么我们就可以看看目标串从4位置hash到 1位置的hash跟当前路径的hash值等否?如果相等,那么需要一段什么样的值呢?
需要一个后缀,从d开始,走到g这段的hash值,是否在前面的搜索过程中出现过,如果出现过,那么好了a到d的有了,d到g的也有了,那么a到g的就有了,答案就找到了。
 #include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
const int maxn = ;
const ULL Base = ;
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
}e[maxn<<];
bool vis[maxn];
int head[maxn],sz[maxn],maxson[maxn],pre[maxn],suf[maxn],tot,len,cnt;
ULL Pre[maxn],Suf[maxn],B[maxn] = {};
char sa[maxn],sb[maxn];
void add(int u,int v){
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void dfs(int u,int fa){
sz[u] = ;
maxson[u] = ;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to]) continue;
dfs(e[i].to,u);
sz[u] += sz[e[i].to];
maxson[u] = max(maxson[u],sz[e[i].to]);
}
}
int FindRoot(int sum,int u,int fa){
int ret = u;
maxson[u] = max(maxson[u],sum - sz[u]);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to]) continue;
int x = FindRoot(sum,e[i].to,u);
if(maxson[x] < maxson[ret]) ret = x;
}
return ret;
}
bool cao(int u,ULL w,int fa,int depth,bool op) {
if(depth > len) return false;
w = w*Base + (ULL)sa[u];
if(op) {
if(w == Pre[depth] && suf[depth] == cnt) return true;
if(w == Suf[len - depth + ] && pre[len - depth + ] == cnt) return true;
} else {
if(w == Pre[depth]) pre[depth] = cnt;
if(w == Suf[len - depth + ]) suf[len - depth + ] = cnt;
}
for(int i = head[u]; ~i; i = e[i].next) {
if(vis[e[i].to] || e[i].to == fa) continue;
if(cao(e[i].to,w,u,depth + ,op)) return true;
}
return false;
}
bool solve(int u){
dfs(u,);
if(sz[u] < len) return false;
int root = FindRoot(sz[u],u,);
vis[root] = true;
++cnt;
ULL w = sa[root];
if(w == Pre[len]) return true;
if(w == Pre[]) pre[] = cnt;
if(w == Suf[len]) suf[len] = cnt;
for(int i = head[root]; ~i; i = e[i].next){
if(vis[e[i].to]) continue;
if(cao(e[i].to,w,root,,true)) return true;
cao(e[i].to,w,root,,false);
}
for(int i = head[root]; ~i; i = e[i].next){
if(vis[e[i].to]) continue;
if(solve(e[i].to)) return true;
}
return false;
}
int main(){
int kase,n,u,v,cs = ;
for(int i = ; i < maxn; ++i) B[i] = B[i-]*Base;
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
memset(head,-,sizeof head);
memset(vis,false,sizeof vis);
memset(pre,,sizeof pre);
memset(suf,,sizeof suf);
tot = ;
for(int i = ; i < n; ++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
scanf("%s%s",sa + ,sb + );
len = strlen(sb + );
for(int i = ; i <= len; ++i) Pre[i] = Pre[i-] + B[i-]*sb[i];
Suf[len + ] = ;
for(int i = len; i > ; --i) Suf[i] = Suf[i+] + B[len - i]*sb[i];
printf("Case #%d: %s\n",cs++,solve()?"Find":"Impossible");
}
return ;
}

HDU 5469 Antonidas的更多相关文章

  1. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  2. hdu 5469 Antonidas (dfs+剪枝)2015 ACM/ICPC Asia Regional Shanghai Online

    题意: 给出一棵树,再给出每个节点上的值(一个char字符)这些值以一个字符串s1表示,然后给出一个s2字符串,问在这棵树上是否存在两个点,从一个点走到另一个点所经过的路径上的char字符组成的字符串 ...

  3. HDU 5469 Antonidas (树形DP,暴力)

    题意: 给一棵n节点的树图,每个点都是一个小写字母,要求找到两个点(a,b),从a->b的路径上形成了一个字符串为s.给出s,问是否存在这样的点对. 思路: 考虑一个点,要么从该点出发,要么在该 ...

  4. 【HDU5469】Antonidas(点分治,字符串哈希)

    [HDU5469]Antonidas(点分治,字符串哈希) 题面 HDU Vjudge 题解 啊哈?什么垃圾一眼点分治+Hash判断,哈哈哈哈哈,让我来码码码. 诶,怎么WA了.改改改改改. 诶,怎么 ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. h5-18-文件操作-兼容判断

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Eclipse Neon Java版本安装Java EE插件

    Help→Install New Software 地址:Neon - http://download.eclipse.org/releases/neon/201703231000 选择  Web,X ...

  3. HDU 1223 打表 + 大数

    http://acm.hdu.edu.cn/showproblem.php?pid=1223 一般遇到这些题,我都是暴力输出前几项,找规律.未果. 然后输出n = 1时候,以A开始,有多少个答案, n ...

  4. python中函数参数

    默认参数注意点 优点:灵活,当没有指定与形参对应的实参时就会使用默认参数 缺陷: 例子: >>> def h(m, l=[]):                    #默认参数时列 ...

  5. Oracle及其相关软件历史版本下载地址

    https://edelivery.oracle.com/osdc/faces/Home.jspx 打开上面这个链接,输入自己或可用的帐号即可. 搜索到自己想要下载的软件后,点击,软件会添加到购物车中 ...

  6. AJPFX总结jvm运行时内存分布

    jvm的运行过程中将java程序运行时数据区分为以下几个部分:      (1)程序计数器:存储虚拟机字节码执行的地址 (2)java虚拟机栈:java方法运行时的局部变量表,操作数栈,方法出口等 ( ...

  7. 微信小程序 插件介绍

    小程序的插件是对一组js接口.自定义组件或页面的封装.插件不能独立运行,必须嵌入在其他小程序中才能被用户使用:而第三方小程序在使用插件时,也无法看到插件的代码.因此,插件适合用来封装自己的功能或服务, ...

  8. Bootstrap基本理论

    Bootstrap,来自 Twitter,是目前最受欢迎的前段框架.Bootstrap是基于HTML.CSS.JAVASCRIPT的,它简洁灵活,使得Web开发更加快捷 Bootstrap特点:优雅, ...

  9. chrome inspect出现白屏的解决方案

    点inspect后 弹出框,可是里面一片白色 PS:原效果不是这样,只是图找不到随便p的 原因可以看这个:http://www.cnblogs.com/slmk/p/7591126.html 大概意思 ...

  10. How to proxy a web site by apache2 in Ubuntu

    Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...