【2017 ACM/ICPC Asia Regional Qingdao Online 1003】hdu 6208 The Dominator of Strings
来源:互联网 发布:backbone.js淘汰了吗 编辑:程序博客网 时间:2024/06/05 20:32
The Dominator of Strings
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 753 Accepted Submission(s): 219
Total Submission(s): 753 Accepted Submission(s): 219
Problem Description
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The stringS is dominated by T if S is a substring of T .
Input
The input contains several test cases and the first line provides the total number of cases.
For each test case, the first line contains an integerN indicating the size of the set.
Each of the followingN lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of100000 .
The limit is 30MB for the input file.
For each test case, the first line contains an integer
Each of the following
The total length of strings in each case has the limit of
The limit is 30MB for the input file.
Output
For each test case, output a dominator if exist, or No if not.
Sample Input
310youbetterworsericherpoorersicknesshealthdeathfaithfulnessyoubemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness5abccdeabcdeabcdebcde3aaaaaaaaabaaaac
Sample Output
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulnessabcdeNo
AC自动机
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <string>#include <cstring>#include <ctime>#include <queue>#define ms(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long ll;const int N = 100010;const int M = 26;struct AC{ int ch[N][M], fail[N], val[N]; int sz; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); memset(val, 0, sizeof(val)); } int idx(char c) { return c - 'a'; } void add(char *s) { int n, now = 0; n = strlen(s); for (int i = 0; i < n; i++) { int id = idx(s[i]); if (ch[now][id] == 0) { memset(ch[sz], 0, sizeof(ch[sz])); ch[now][id] = sz++; } now = ch[now][id]; } val[now]++; } void Build() { queue<int>que; fail[0] = 0; for (int i = 0; i < M; i++) { int u = ch[0][i]; if (u) { fail[u] = 0; que.push(u); } } while (!que.empty()) { int r = que.front(); que.pop(); for (int c = 0; c < M; c++) { int u = ch[r][c]; if (!u) { continue; } que.push(u); int v = fail[r]; while (v && !ch[v][c]) { v = fail[v]; } fail[u] = ch[v][c]; } } } int Query(char *s) { int n, now = 0, ans = 0; n = strlen(s); for (int i = 0; i < n; i++) { int id = idx(s[i]); while (now && !ch[now][id]) { now = fail[now]; } now = ch[now][id]; int t = now; while (t && val[t] != -1) { ans += val[t]; val[t] = -1; t = fail[t]; } } return ans; }} ac;char s[100010], a[100010];int main(){ ios::sync_with_stdio(false); int T, n; cin >> T; while (T--) { cin >> n; if (n == 0) { cout << "No" << endl; continue; } int maxx = 0, hh = n, len; ac.init(); while (hh--) { cin >> a; len = strlen(a); ac.add(a); if (len > maxx) { maxx = len; strcpy(s, a); s[len] = '\0'; } } ac.Build(); if (ac.Query(s) == n) { cout << s << endl; } else { cout << "No" << endl; } } return 0;}
阅读全文
0 0
- 【2017 ACM/ICPC Asia Regional Qingdao Online 1003】hdu 6208 The Dominator of Strings
- HDU-2017 ACM/ICPC Asia Regional Qingdao Online-1003-The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Qingdao Online:1003 The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Qingdao Online C The Dominator of Strings
- HDU6208 The Dominator of Strings 字符串匹配 2017 ACM/ICPC Asia Regional Qingdao Online
- HDU6208 The Dominator of Strings AC自动机 [2017 ACM/ICPC Asia Regional Qingdao Online]
- HDu 6216 && 2017 ACM/ICPC Asia Regional Qingdao Online 1011
- HDU-2017 ACM/ICPC Asia Regional Qingdao Online-1001-Apple
- HDU-2017 ACM/ICPC Asia Regional Qingdao Online-补题
- 2017 ACM/ICPC Asia Regional Qingdao Online
- 2017 ACM/ICPC Asia Regional Qingdao Online
- 2017 ACM/ICPC Asia Regional Qingdao Online
- 2017 ACM/ICPC Asia Regional Qingdao Online
- HDU 5879 Cure 2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5884 Sort 2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5878 -- 丑数打表(2016 ACM/ICPC Asia Regional Qingdao Online)
- HDU 5878 -- 丑数打表(2016 ACM/ICPC Asia Regional Qingdao Online)
- HDU 6216 A Cubic number and A Cubic Number
- 学习java的一些简单知识
- 实验一报告
- pycharm profession edition破解
- 中缀式变后缀式
- 【2017 ACM/ICPC Asia Regional Qingdao Online 1003】hdu 6208 The Dominator of Strings
- R语言RSNNS
- 警匪110
- 百度地图定位
- Ubuntu16.04网速慢
- No system images installed for this target 解决办法
- 开发环境搭建
- java Spring @RequestMapping
- 字符串ASCII码小应用