hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
题目链接:http://csustacm.com:4803/problem/1006
题目:


思路:正如题目一样,本题是一个hash,比赛的时候用的字典树,但是不知道为什么一直RE(听学长说要动态开点,但是没学字典树,瞎套的板子,可能真的是我姿势不对吧~),赛后学了一边hash(字符串题只会上星期学的kmp)后把这题补了一下。这题就最后比较时需要将每个节点的从1到该节点路径上所有的字母组成一个新的字符串,然后与题目给的n个字符串进行匹配(匹配定义题目中有说),问是否所有的字符串都至少与给的n个字符串中的一个匹配,因此我们需要借助dfs来处理新的串,我们用一个vis来标记原来那n个字符串的字串的hash值,在跑dfs时顺便判定,降低复杂度。
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f;
const ull p = ; unordered_map<ull, int> vis;
int n, m, u, v, k, x;
int w[maxn];
ull hs1, hs2[maxn];
int tot;
int head[maxn]; struct edge {
int v, next;
}ed[maxn<<]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
ed[tot].v = u;
ed[tot].next = head[v];
head[v] = tot++;
} bool dfs(int u, int fa) {
for(int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if(v == fa) continue;
hs2[v] = hs2[u] * p + w[v];
if(!vis[hs2[v]]) return false;
if(!dfs(v, u)) return false;
}
return true;
} int main() {
//FIN;
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
for(int i = ; i <= n; i++) {
scanf("%d", &k);
hs1 = ;
for(int j = ; j <= k; j++) {
scanf("%d", &x);
hs1 = hs1 * p + x;
vis[hs1] = ;
}
}
for(int i = ; i <= m; i++) {
scanf("%d", &w[i]);
}
for(int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
}
if(!vis[w[]]) {
puts("NO");
} else {
hs2[] = w[];
if(dfs(, )) puts("YES");
else puts("NO");
}
return ;
}
补字典树写法:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int len, root, k, m, n, x, y;
int w[maxn];
vector<int> v, G[maxn]; struct Trie {
int cnt;
map<int, int > nxt; void init() {
cnt = ;
nxt.clear();
}
}T[maxn]; void insert(int s) {
int now = root;
for(auto x : v[s]) {
if(T[now].nxt[x] == ) {
T[len].init();
T[now].nxt[x] = len++;
}
now = T[now].nxt[x];
T[now].cnt++;
}
} bool dfs(int u, int p, int now) {
int res = ;
for(auto v : G[u]) {
if(v == p) continue;
if(T[now].nxt[w[v]] == ) return ;
res &= dfs(v, u, T[now].nxt[w[v]]);
}
return res;
} int main() {
FIN;
scanf("%d%d", &n, &m);
len = , root = ;
T[].init();
for(int i = ; i <= n; i++) {
scanf("%d", &k);
for(int j = ; j <= k; j++) {
scanf("%d", &x);
v[i].push_back(x);
}
insert(i);
}
for(int i = ; i <= m; i++) {
scanf("%d", &w[i]);
}
for(int i = ; i < m; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
if(T[].nxt[w[]] == ) puts("NO");
else {
if(dfs(, , T[].nxt[w[]])) puts("YES");
else puts("NO");
}
return ;
}
hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)的更多相关文章
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- 2018.9.9 nowcoder 普及组第一场
2018.9.9 nowcoder 普及组第一场 C-括号 题目大意:一个只包含左右括号的字符串\(S\),希望删掉S中若干个字符,使得剩下的字符串是一个合法的括号串,有多少不同的方案. Soluti ...
- 2020ICPC·小米 网络选拔赛第一场
2020ICPC·小米 网络选拔赛第一场 C-Smart Browser #include <string> #include <iostream> std::string s ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 校省选赛第一场A题Cinema题解
今天是学校省选的第一场比赛,0战绩收工,死死啃着A题来做,偏偏一直WA在TES1. 赛后,才发现,原来要freopen("input.txt","r",stdi ...
- 旅游(CSUST省赛选拔赛2+状压dp+最短路)
题目链接:http://csustacm.com:4803/problem/1016 题目: 思路:状压dp+最短路,比赛的时候有想到状压dp,但是最短路部分写挫了,然后就卡死了,对不起出题人~dis ...
- 2019牛客多校赛第一场 补题 I题
I题 Points Division 题意: 给你n个点,每个点有坐标(xi,yi)和属性(ai,bi),将点集划分为两个集合, 任意 A 集合的点 i 和 B 集合点 j, 不允许 xi > ...
- 2018 计蒜之道-初赛 第一场 A-百度无人车
百度一共制造了 nn 辆无人车,其中第 ii 辆车的重量为 a_i\ \mathrm{kg}ai kg. 由于车辆过重会增大轮胎的磨损程度,现在要给这 nn 辆车减轻重量.每将一辆车减轻 1\ \m ...
随机推荐
- 复利计算程序单元测试(C语言)
对我们和复利计算程序,写单元测试. 有哪些场景? 期待的返回值 写测试程序. 运行测试. 我的复利计算程序是用C语言写的,不懂使用C语言的测试工具,所以用C语言的运行结果来反映测试结果. 测试模块(场 ...
- Hibernate 中一级缓存和快照区的理解
刚刚开始的时候觉得这个快照区很难理解,在网上看了很多博客之后,开始明白了.我是结合 ADO.NET 理解的,在ADO.NET 中有一个类, 叫 SqlCommandBuilder,在我看来,他就是 A ...
- MVC与MVP简单对比
在Java平台,基于Spring等技术的MVC框架已经走向成熟:在.NET平台,微软也推出了MVC.MVP Framework,MVP不同于MVC的地方,关键在于,View不再显示的依赖于Busine ...
- scrollTop()案例
设置 <div> 元素中滚动条的垂直偏移: 定义和用法 scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置. scroll top offset 指的是滚动条相对于其顶部的偏 ...
- BZOJ4974 字符串大师(kmp)
显然最短循环节长度=i-next[i],则相当于给定next数组构造字符串.然后按照kmp的过程模拟即可.虽然这看起来是一个染色问题,但是由图的特殊性,如果next=0只要贪心地选最小的就可以了,稍微 ...
- CF530D sum in the tree
我是题面.原题地址 很简单的一道贪心题 首先,先想想怎么判断是否合法 题目中说,a是自然数,那么子节点的s明显是不能比父节点大的,如果比父节点大,不合法! 所有深度为偶数的点的s被删除了,也只有深度为 ...
- 802.1p 优先级与内部优先级的映射关系
缺省情况下,所有华为 S 系列交换机的 802.1P 优先级 与内部优先级的映射关系是 一 样的,如表 10-3 所示.从中可以看出,这些交换机中 802.1p 优先级与内部优先级的缺省映射关系是按等 ...
- SAM I AM UVA - 11419(最小顶点覆盖+输出一组解)
就是棋盘问题输出一组解 https://blog.csdn.net/llx523113241/article/details/47759745 http://www.matrix67.com/blog ...
- 转:python的nltk中文使用和学习资料汇总帮你入门提高
python的nltk中文使用和学习资料汇总帮你入门提高 转:http://blog.csdn.net/huyoo/article/details/12188573 nltk的安装 nltk初步使用入 ...
- PyCharm入门教程——在编辑器中打开和重新打开文件
PyCharm最新版本下载 JetBrains PyCharm是一种Python IDE,其带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具.此外,该IDE提供了一些高级功能,以用于 ...