hdu6208 The Dominator of Strings
地址:
题目:
The Dominator of Strings
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 857 Accepted Submission(s): 264
For each test case, the first line contains an integer N indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000.
The limit is 30MB for the input file.
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac
abcde
No
#include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
using namespace std; struct AC_auto
{
const static int LetterSize = ;
const static int TrieSize = * ( 1e5 + ); int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize]; int newnode(void)
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = ;
return tot++;
} void init(void)
{
tot = ;
root = newnode();
} int getidx(char x)
{
return x - 'a';
} void insert(string &ss)
{
int len = ss.length();
int now = root;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
if(next[now][idx] == -)
next[now][idx] = newnode();
now = next[now][idx];
}
end[now]++;
} void build(void)
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < LetterSize; i++)
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root,Q.push(next[root][i]);
while(Q.size())
{
int now = Q.front();Q.pop();
for(int i = ; i < LetterSize; i++)
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else
fail[next[now][i]] = next[fail[now]][i],Q.push(next[now][i]);
}
} int match(string &ss)
{
int len,now,res;
len = ss.length(),now = root,res = ;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
int tmp = now = next[now][idx];
while(tmp)
{
res += end[tmp];
end[tmp] = ;//按题目修改
tmp = fail[tmp];
}
}
return res;
}
void debug()
{
for(int i = ;i < tot;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = ;j < LetterSize;j++)
printf("%3d",next[i][j]);
printf("]\n");
}
}
};
AC_auto ac;
string ss[];
int main(void)
{
std::ios::sync_with_stdio();
cin.tie();
//freopen("in.acm","r",stdin);
int t,n,id;cin>>t;
while(t--)
{
id=;
ac.init();
cin>>n>>ss[];
for(int i=;i<=n;i++) cin>>ss[i],id=ss[id].length()>ss[i].length()?id:i;
for(int i=;i<=n;i++)
if(i!=id)
ac.insert(ss[i]);
ac.build();
if(ac.match(ss[id])==n-)
cout<<ss[id]<<"\n";
else
cout<<"No\n";
}
return ;
}
sam做法:
#include <bits/stdc++.h> using namespace std; struct SAM
{
static const int MAXN = <<;//大小为字符串长度两倍
static const int LetterSize = ; int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
int sum[MAXN], tp[MAXN], cnt[MAXN]; //sum,tp用于拓扑排序,tp为排序后的数组 void init( void)
{
last = tot = ;
len[] = ;
memset(ch[],,sizeof ch[]);
memset(cnt,,sizeof cnt);
} void add(int x)
{
int p = last, np = last = ++tot;
len[np] = len[p] + , cnt[last] = ;
memset(ch[np],,sizeof ch[np]);
while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
if( p == )
fa[np] = ;
else
{
int q = ch[p][x];
if( len[q] == len[p] + )
fa[np] = q;
else
{
int nq = ++tot;
memcpy( ch[nq], ch[q], sizeof ch[q]);
len[nq] = len[p] + , fa[nq] = fa[q], fa[q] = fa[np] = nq;
while( p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
}
}
} void toposort( void)
{
for(int i = ; i <= len[last]; i++) sum[i] = ;
for(int i = ; i <= tot; i++) sum[len[i]]++;
for(int i = ; i <= len[last]; i++) sum[i] += sum[i-];
for(int i = ; i <= tot; i++) tp[sum[len[i]]--] = i;
for(int i = tot; i; i--) cnt[fa[tp[i]]] += cnt[tp[i]];
} bool match(string &s)
{
for(int i=,p=;i<s.length();p=ch[p][s[i]-'a'],i++)
if(!ch[p][s[i]-'a'])
return ;
return ;
}
} sam;
string ss[];
int main(void)
{
ios::sync_with_stdio();
cin.tie();
int t;cin>>t;
while(t--)
{
int n,ff=,id=;
cin>>n;
for(int i=;i<=n;i++) cin>>ss[i],id=(ss[i].length()>ss[id].length()?i:id);
sam.init();
for(int i=;i<=n;i++)
if(i!=id)
{
for(int j=;j<ss[i].length();j++) sam.add(ss[i][j]-'a');
sam.add();
}
for(int i=n;i&&ff;i--)
if(i!=id&&!sam.match(ss[i]))
ff=;
if(ff) cout<<ss[id]<<"\n";
else cout<<"No\n";
}
return ;
}
hdu6208 The Dominator of Strings的更多相关文章
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- HDU 6208 The Dominator of Strings(AC自动机)
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings hdu 6208
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】
Problem Description Here you have a set of strings. A dominator is a string of the set dominating al ...
- HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机
https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...
- The Dominator of Strings HDU - 6208(ac自动机板题)
题意: 就是求是否有一个串 是其它所有串的母串 解析: 把所有的串都加入到trie数中 然后用最长的串去匹配就好了 emm..开始理解错题意了...看成了只要存在一个串是另一个的母串就好.. 然后输 ...
- 【hdu 6208】The Dominator of Strings
[链接]h在这里写链接 [题意] 问你n个串里面有没有一个串,使得其余n-1个串都是他的子串. [题解] 后缀数组. 答案肯定是那个最长的串. 则,把那个串求一下Sa数组(注意仅仅那个最长的串求). ...
- HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)
最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...
随机推荐
- sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)
Ivan comes again! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...
- Asp.net - Razor - 将Model中变量的值赋值给Javascript变量
<script type="text/javascript"> @{var _userID = Model.UserId.HasValue ? Model.UserId ...
- DecimalFormat 四舍五入Float类型的坑
今天又踩了一个坑,使用DecimalFormat来完毕四舍五入.可是传入的是float类型,几轮測试才发现一个问题,传入的float会被转为double类型.大家都知道float是4位,double是 ...
- TArray<uint8>转FString
void ARamaUDPReceiver::Recv(const FArrayReaderPtr& ArrayReaderPtr, const FIPv4Endpoint& EndP ...
- MVC模式 与 Model2模型 介绍
Model1回顾 MVC模式:MVC(Model.View.Controller)是软件开发过程中比较流行的设计思想.旨在分离模型.控制.师徒.是一种分层思想的体现. Model2简介Java Web ...
- Firefox 新增容器标签:可同时登录多个用户
Mozilla昨天在Firefox夜间构建版50.0a1中增加了一个名为“容器标签Container Tabs”的实验性功能. Mozilla的工程师称,该功能可以将用户的浏览会话分到不同的容器中.这 ...
- java基础---->Java中异常的使用(二)
这一篇博客用例子讲述一下异常的处理过程.那些 我们一直惴惴不安 又充满好奇的未来 会在心里隐隐约约地觉得它们是明亮的. 异常的执行过程 一.实例一:return语句 public class Exce ...
- 【BZOJ3781、2038】莫队算法2水题
[BZOJ3781]小B的询问 题意:有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...
- 浏览器出现“ net::ERR_BLOCKED_BY_CLIENT”错误的解决方法
转载自:http://www.dbmng.com/art-2136.html Failed to load resource: net::ERR_BLOCKED_BY_CLIENT错误报告 错误原因: ...
- Linux中配置主机之间的免密ssh登陆
假如 A 要登陆 B在A上操作:1.首先生成密钥对 ssh-keygen (提示时,直接回车即可) 2.再将A自己的公钥拷贝并追加到B的授权列表文件authorized_keys中 ssh-copy- ...