题目链接: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+字典树)的更多相关文章

  1. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  2. 2018.9.9 nowcoder 普及组第一场

    2018.9.9 nowcoder 普及组第一场 C-括号 题目大意:一个只包含左右括号的字符串\(S\),希望删掉S中若干个字符,使得剩下的字符串是一个合法的括号串,有多少不同的方案. Soluti ...

  3. 2020ICPC·小米 网络选拔赛第一场

    2020ICPC·小米 网络选拔赛第一场 C-Smart Browser #include <string> #include <iostream> std::string s ...

  4. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  5. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  6. 校省选赛第一场A题Cinema题解

    今天是学校省选的第一场比赛,0战绩收工,死死啃着A题来做,偏偏一直WA在TES1. 赛后,才发现,原来要freopen("input.txt","r",stdi ...

  7. 旅游(CSUST省赛选拔赛2+状压dp+最短路)

    题目链接:http://csustacm.com:4803/problem/1016 题目: 思路:状压dp+最短路,比赛的时候有想到状压dp,但是最短路部分写挫了,然后就卡死了,对不起出题人~dis ...

  8. 2019牛客多校赛第一场 补题 I题

    I题  Points Division 题意: 给你n个点,每个点有坐标(xi,yi)和属性(ai,bi),将点集划分为两个集合, 任意 A 集合的点 i 和 B 集合点 j, 不允许 xi > ...

  9. 2018 计蒜之道-初赛 第一场 A-百度无人车

    百度一共制造了 nn 辆无人车,其中第 ii 辆车的重量为 a_i\ \mathrm{kg}ai​ kg. 由于车辆过重会增大轮胎的磨损程度,现在要给这 nn 辆车减轻重量.每将一辆车减轻 1\ \m ...

随机推荐

  1. 使用union all 遇到的问题(俩条sql语句行数的和 不等于union all 后的 行数的和 !);遗留问题 怎么找到 相差的呐俩条数据 ?

    create table buyer as SELECT b.id AS bankid FROM v_product_deal_main m, base_member b WHERE b.id = m ...

  2. mysql(四)log

    慢查询: https://blog.csdn.net/leshami/article/details/39829605 日志组成: https://blog.csdn.net/leshami/arti ...

  3. Arrays.toString 如果传入的是对象 那么调用的是此对象的toString

    Arrays.toString(Object[] obj) 如果传入参数的是对象 那么调用的是此对象的toString

  4. BZOJ4925 城市规划

    对每个人行道求出移动距离在哪些区间内时其在建筑物前面.现在问题即为选一个点使得其被最多的区间包含.差分即可.对建筑暴力去掉重叠部分.开始时没有去重用了nm次vector的push_back,时间大概是 ...

  5. C++解析(5):内联函数分析

    0.目录 1.常量与宏回顾 2.内联函数 3.内联函数深度探析 4.注意事项 5.小结 1.常量与宏回顾 C++中的const常量可以替代宏常数定义,如: const int A = 3; <- ...

  6. Guardian of Decency UVALive - 3415(最大独立集板题)

    老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学生数量 ...

  7. 【MediaElement】WPF视频播放器【3】

    一.前言 对于<MediaElement>前两章介绍了差不多了,其实好的界面还需要UI工程师的配合,比如帮忙设计下按钮的样式等等.同样视频本身也需要吸引人,不然做的再好的播放器也没用.之后 ...

  8. 洛谷 P3924 康娜的线段树 解题报告

    P3924 康娜的线段树 题目描述 小林是个程序媛,不可避免地康娜对这种人类的"魔法"产生了浓厚的兴趣,于是小林开始教她\(OI\). 今天康娜学习了一种叫做线段树的神奇魔法,这种 ...

  9. 代码收藏系列--javascript--日期函数

    /** * 获取当前时间的简短函数 * @returns {String} * @@example getTimeStamp() 结果是:2017-07-12 09:21:30 */ function ...

  10. 【noip2018】【luogu5024】保卫王国

    题目描述 Z 国有nn座城市,n - 1n−1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需要满足如下几个条 ...