题意:

给出两个字符串a,b,求一个字符串,这个字符串是a和b的子串,

且只在a,b中出现一次,要求输出这个字符串的最小长度。

题解:

将a串放入后缀自动机中,然后记录一下每个节点对应的子串出现的次数

然后把b串取自动机中匹配

然后判断一下

 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = ;
char s[maxn];
int Q; struct Suffix_Automaton {
int last, tot, nxt[maxn << ][], fail[maxn << ];//last是未加入此字符前最长的前缀(整个串)所属的节点的编号
int len[maxn << ];// 最长子串的长度 (该节点子串数量 = len[x] - len[fa[x]])
int sa[maxn << ], c[maxn << ];
int sz[maxn << ];// 被后缀链接的个数,方便求节点字符串的个数
LL num[maxn << ];// 该状态子串的数量
LL maxx[maxn << ];// 长度为x的子串出现次数最多的子串的数目
LL sum[maxn << ];// 该节点后面所形成的自字符串的总数
LL subnum, sublen;// subnum表示不同字符串数目,sublen表示不同字符串总长度
int X[maxn << ], Y[maxn << ]; // Y表示排名为x的节点,X表示该长度前面还有多少个
int minn[maxn << ], mx[maxn << ];//minn[i]表示多个串在后缀自动机i节点最长公共子串,mx[i]表示单个串的最长公共子串
void init() {
tot = last = ;
fail[] = len[] = ;
for (int i = ; i < ; i++) nxt[][i] = ;
} void extend(int c) {
int u = ++tot, v = last;
len[u] = len[v] + ;
num[u] = ;
for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u;
if (!v) fail[u] = , sz[]++;
else if (len[nxt[v][c]] == len[v] + ) fail[u] = nxt[v][c], sz[nxt[v][c]]++;
else {
int now = ++tot, cur = nxt[v][c];
len[now] = len[v] + ;
memcpy(nxt[now], nxt[cur], sizeof(nxt[cur]));
fail[now] = fail[cur];
fail[cur] = fail[u] = now;
for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now;
sz[now] += ;
}
last = u;
//return len[last] - len[fail[last]];//多添加一个子串所产生不同子串的个数
} void get_num() {// 每个节点子串出现的次数
for (int i = ; i <= tot; i++) X[len[i]]++;
for (int i = ; i <= tot; i++) X[i] += X[i - ];
for (int i = ; i <= tot; i++) Y[X[len[i]]--] = i;
for (int i = tot; i >= ; i--) num[fail[Y[i]]] += num[Y[i]];
} void get_maxx(int n) {// 长度为x的子串出现次数最多的子串的数目
get_num();
for (int i = ; i <= tot; i++) maxx[len[i]] = max(maxx[len[i]], num[i]);
} void get_sum() {// 该节点后面所形成的自字符串的总数
get_num();
for (int i = tot; i >= ; i--) {
sum[Y[i]] = ;
for (int j = ; j <= ; j++)
sum[Y[i]] += sum[nxt[Y[i]][j]];
}
} void get_subnum() {//本质不同的子串的个数
subnum = ;
for (int i = ; i <= tot; i++) subnum += len[i] - len[fail[i]];
} void get_sublen() {//本质不同的子串的总长度
sublen = ;
for (int i = ; i <= tot; i++) sublen += 1LL * (len[i] + len[fail[i]] + ) * (len[i] - len[fail[i]]) / ;
} void get_sa() { //获取sa数组
for (int i = ; i <= tot; i++) c[len[i]]++;
for (int i = ; i <= tot; i++) c[i] += c[i - ];
for (int i = tot; i >= ; i--) sa[c[len[i]]--] = i;
} int cntnum[maxn << ]; void match() {//多个串的最长公共子串
mem(cntnum, );
int n = strlen(s), p = , ans = INF;
for (int i = ; i < n; i++) {
int c = s[i] - 'a';
if (nxt[p][c]) p = nxt[p][c];
else {
for (; p && !nxt[p][c]; p = fail[p]);
if (!p) p = ;
else p = nxt[p][c];
}
cntnum[p]++;
}
for (int i = tot; i; i--) cntnum[fail[Y[i]]] += cntnum[Y[i]];
for (int i = ; i <= tot; i++)
if (num[i] == && cntnum[i] == ) ans = min(ans, len[fail[i]] + );
if (ans == INF) printf("-1\n");
else printf("%d\n", ans);
} void get_kth(int k) {//求出字典序第K的子串
int pos = , cnt;
string s = "";
while (k) {
for (int i = ; i <= ; i++) {
if (nxt[pos][i] && k) {
cnt = nxt[pos][i];
if (sum[cnt] < k) k -= sum[cnt];
else {
k--;
pos = cnt;
s += (char) (i + 'a');
break;
}
}
}
}
cout << s << endl;
} } sam; int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
sam.init();
sfs(s + );
int n = strlen(s + );
for (int i = ; i <= n; i++) sam.extend((s[i] - 'a'));
sfs(s);
sam.get_num();
sam.match();
#ifndef ONLINE_JUDGE
cout << "Totle Time : " << (double) clock() / CLOCKS_PER_SEC << "s" << endl;
#endif
return ;
}

Match & Catch CodeForces - 427D 后缀自动机水题的更多相关文章

  1. CF873F Forbidden Indices 后缀自动机+水题

    刷刷水~ Code: #include <cstdio> #include <cstring> #include <algorithm> #define N 200 ...

  2. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题

    链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...

  3. BZOJ 5084: hashit 后缀自动机(原理题)

    比较考验对后缀自动机构建过程的理解. 之前看题解写的都是树链的并,但是想了想好像可以直接撤销,复杂度是线性的. 自己想出来的,感觉后缀自动机的题应该不太能难倒我~ 注意:一定要手画一下后缀自动机的构建 ...

  4. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  5. 【BZOJ5084】hashit(后缀自动机水过)

    点此看题面 大致题意: 每次在字符串后面加入或删除一个字符,求本质不同的子串个数. 后缀自动机 先说明,此题后缀自动机的确能过. 但我的后缀自动机比较弱,遇上一个较强的\(Hack\)数据就被卡掉了. ...

  6. Cyclical Quest CodeForces - 235C 后缀自动机

    题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...

  7. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  8. spoj - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

  9. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

随机推荐

  1. 力扣算法题—145BinartTreePostorderTraversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  2. Java类成员访问级别

    类可以是public或default(或包级别).在本教程中主要介绍 public,private,protected 这几个访问级别修辞符的使用.类成员的访问级别确定程序的哪个区域可以访问它. 以下 ...

  3. 开启MySQL远程访问权限 允许远程连接(阿里云服务器)、linux关闭防火墙

    1.登陆mysql:mysql -u root -p mysql> show databases; +--------------------+ | Database | +---------- ...

  4. zookeeper的监听

    统一配置管理.统一命名服务.分布式锁.集群管理

  5. php中mkdir()函数的权限问题(转)

    问题描述: 使用以下php代码创建了一个目录,期望目录的权限是0777,实际结果是0755 mkdir('./aa/',0777); 分析与测试结果: 1.mkdir()函数指定的目录权限只能小于等于 ...

  6. python-模块-包

    一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...

  7. Python系列——常用第三方库

    幕布视图(更加方便.明了):https://mubu.com/doc/AqoVZ8x6m0 参考文献:嵩天老师的Python讲义 模块 定义 计算机在开发过程中,代码越写越多,也就越难以维护,所以为了 ...

  8. webservice的使用-axis1-01

    1.搭建axis服务器 1.1 下载axis-bin-1_4.zip文件并解压 1.2 拷贝\axis-1_4\webapps目录下的axis到tomcat目录下的webapps目录下并启动 1.3 ...

  9. myEclipse 搭建 Spring boot+myBatis+maven 项目流程

    1.新建一个工程 new-->maven project-->next-->next-->在filter中搜索webapp-->group id.Artifact id- ...

  10. OC开发系列-内存管理

    概述 移动设备的内存极其有限,每个app所有占用的内存是有限的.当app所占用的内存比较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间. 任何集成了NSObject的对象都需要手动进行 ...