题意:

给出两个字符串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 ;
}

最新文章

  1. windwos server 路由备份和恢复 路由表备份和恢复
  2. Object.create
  3. 【转】Flume日志收集
  4. 解决【无法启动此程序,因为计算机中丢失MSVCP71.dll。尝试重新安装该程序以解决此问题】问题
  5. ACM Color the fence
  6. [Unity3D]脚本中Start()和Awake()的区别
  7. Oracle Dataguard Standby Redo Log的两个实验
  8. Educational Codeforces Round 5 E. Sum of Remainders (思维题)
  9. FontDialog组件设置字体
  10. jquery and event
  11. VLC-Android和VLC几个关键宏定义的分析
  12. java整体集合框架
  13. Python-常用 Linux 命令的基本使用
  14. pytest进阶之xunit fixture
  15. java枚举类型详解
  16. spring 动态代理
  17. 环境搭建文档——Windows下的Python3环境搭建
  18. ADB命令(全)
  19. 2、haproxy配置参数详解
  20. C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新)

热门文章

  1. Git比较分支差异的3个命令
  2. 论一个PHP项目上线的注意点
  3. Vue手把手教你撸一个 beforeEnter 钩子函数
  4. 为什么javaBean要有get/set方法的设计
  5. 更换nginx默认端口以及配置文件位置
  6. React 16.4 生命周期
  7. Java面试宝典(2)Java基础部分
  8. Failed! Error: Unknown error 1130
  9. myeclipse中使用maven插件的时候,报错-Dmaven.multiModuleProjectDirectory system propery is not set.
  10. android绑定usb前后摄像头