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. 线段树(单点更新) HDU 1754 I Hate It

    题目传送门 /* 线段树基本功能:区间最大值,修改某个值 */ #include <cstdio> #include <cstring> #include <algori ...

  2. 什么是极坐标? —— 一点微小的想法 What is Polar Coordinate ? - Some Naive Thoughts about It

    Can you answer these three questions? The answer seems to be trivial, since we can use our eyes to o ...

  3. [转]Android TCP长连接 心跳机制及实现

    背景知识 智能手机上的长连接心跳和在Internet上的长连接心跳有什么不同 Android系统的推送和iOS的推送有什么区别 几种推送的实现方式 协议 1XMPP简介 2 MQTT简介 3移动端消息 ...

  4. var、符号运算、条件语句、三元(目)运算、自加和自减

    1.var  a=“hello world” a 这个变量是字符串了,对于里面的每一个字母来说,他是字节,里面有11个字节,(包括空格),字节总数用length表示 2.符号运算 + 字符串拼接 . ...

  5. wkWebView 的一些问题

    导语 WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit 中笨重难用.内存泄漏的 UIWebView.WKWebView 拥有60fps滚动刷 ...

  6. sql地址寻路算法(省市区路)

    最近无意翻开4年前做过的一个功能,就是搜集全国各城市各个区(县)的路(XX路.XX道.XX街.XX镇.XX乡.XX屯.XX村.XX社).众所周知,我们都可以在网上找到省.市.区(县)这三级联动的数据, ...

  7. 汇编3栈帧,参数传递,串操作,混合汇编,x64,asm文件

    基础知识2 选择结构 通过判断 + 条件跳转指令来实现 循环结构 通过判断 + 条件跳转指令来实现(会有一个向上跳转的语句) 函数调用约定 C调用约定: 由外部平衡栈 标准调用约定 : 由函数内部平衡 ...

  8. Zend studio 修改编码格式

    一.临时修改编码格式 edit -> Set Encoding... -> Other(选择) 二.修改软件默认编码格式

  9. k8s 核心功能[转]

    部署应用 执行命令: kubectl run kubernetes-bootcamp \ --image=docker.io/jocatalin/kubernetes-bootcamp:v1 \ -- ...

  10. 插入insert几种用法

    1.insert ignore into 当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回.所以使用ignore请确保语句本身没有问题,否则也会被忽略掉.例如: INSERT I ...