题目链接: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. 1029C语言文法的理解

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  2. 【beta】视频预发布

    beta阶段视频发布地址: 秒拍: http://www.miaopai.com/show/Ivh31LgnAuWELxboH6gl7g__.htm

  3. collection 多态 会自动转型为子类 继承多态需要显示转型

  4. pyhcarm github

    1.主题 介绍如何用Pycharm实打实的创建.运行.调试程序. 2.准备工作 Pycharm版本为2.7或者更高. 至少安装一个Python解释器,2.4到3.3均可 3.下载安装Pycharm 下 ...

  5. [ZJOI2014]力 FFT

    题面 题解: \[F_j = \sum_{i < j}\frac{q_iq_j}{(i - j)^2} - \sum_{i > j}{\frac{q_iq_j}{(i - j)^2}}\] ...

  6. 使当前对象相对于上层DIV 水平、垂直居中定位

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  7. Linux内核分析8

    周子轩 原创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验目的: 使用gdb ...

  8. oracle 时间

    select to_char(to_date(sysdate,'yyyy-mm-dd'),'day') from dual; select to_date('2017-12-31','day') fr ...

  9. “Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常。-解决方法

    注释掉web/app.config中的:

  10. PID控制算法的C语言实现一 PID算法原理

    本系列是转载............. 全部的程序有一个共同点:就是我没认真去调pid的参数 在工业应用中PID及其衍生算法是应用最广泛的算法之一,是当之无愧的万能算法,如果能够熟练掌握PID算法的设 ...