luogu4608 [FJOI2016]所有公共子序列问题
题目描述:
题解:
序列自动机(?)+高精+普及dp。
这个是猫老师的序列自动机(字符串从1开始):
void sol(char *s,int n,int t[N][])
{
memset(t[n],-,sizeof(t[n]));
for(int i=n-;i>=;i--)
{
memcpy(t[i],t[i+],sizeof(t[i]));
t[i][ci(s[i+])] = i+;
}
}
序列自动机
$t$数组记录在位置$i$的后面加字符$j$能到达的最左边的位置。
对于这道题,直接对两个串构造序列自动机,然后爆搜即可。
$k=1$没啥好说的,$k=0$高精压18位。
时间复杂度$O(n^2)$。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = ;
const ll M = 1000000000000000000ll;
struct bignum
{
ll a[];//
int len;
void operator += (const bignum&x)
{
int mx = max(len,x.len);
for(int i=;i<mx;i++)
{
a[i]+=x.a[i];
if(a[i]>=M)a[i+]+=a[i]/M,a[i]%=M;
}
while(a[len])len++;
}
void operator += (const ll&x)
{
a[]+=x;
for(int i=;a[i]>=M;i++)a[i+]+=a[i]/M,a[i]%=M;
while(a[len])len++;
}
void write()
{
if(!len){puts("");return ;}
printf("%lld",a[len-]);
for(int i=len-;i>=;i--)
printf("%018lld",a[i]);
puts("");
}
}dp[];
int n,m,k,t1[N][],t2[N][],id[N][N],tot=,ans=;
char s1[N],s2[N];
int ci(char ch)
{
if(ch>='A'&&ch<='Z')return ch-'A';
return ch-'a'+;
}
char ic(int i)
{
if(i<)return 'A'+i;
return 'a'+i-;
}
void sol(char *s,int n,int t[N][])
{
memset(t[n],-,sizeof(t[n]));
for(int i=n-;i>=;i--)
{
memcpy(t[i],t[i+],sizeof(t[i]));
t[i][ci(s[i+])] = i+;
}
}
int tl;
char now[N];
void dfs1(int a,int b)
{
for(int i=;i<=tl;i++)
putchar(now[i]);
putchar('\n');
ans++;
for(int i=;i<;i++)if(~t1[a][i]&&~t2[b][i])
{
now[++tl] = ic(i);
dfs1(t1[a][i],t2[b][i]);
tl--;
}
}
void dfs2(int a,int b)
{
if(id[a][b])return ;
id[a][b]=++tot;
dp[tot]+=;
for(int i=;i<;i++)if(~t1[a][i]&&~t2[b][i])
{
dfs2(t1[a][i],t2[b][i]);
dp[id[a][b]]+=dp[id[t1[a][i]][t2[b][i]]];
}
} int main()
{
// freopen("tt.in","r",stdin);
scanf("%d%d%s%s%d",&m,&n,s1+,s2+,&k);
sol(s1,m,t1),sol(s2,n,t2);
if(k==){dfs1(,);printf("%d\n",ans);return ;}
dfs2(,);dp[id[][]].write();
return ;
}
luogu4608 [FJOI2016]所有公共子序列问题的更多相关文章
- 序列自动机—— [FJOI2016]所有公共子序列问题
序列自动机: 是一个处理子序列的自动机.就这样. 建造:(By猫老师:immoralCO猫) s[] next[][] memset(next[n], -, <<); for(int i ...
- 洛谷P4608 [FJOI2016]所有公共子序列问题 【序列自动机 + dp + 高精】
题目链接 洛谷P4608 题解 建个序列自动机后 第一问暴搜 第二问dp + 高精 设\(f[i][j]\)为两个序列自动机分别走到\(i\)和\(j\)节点的方案数,答案就是\(f[0][0]\) ...
- 【LOJ】#2172. 「FJOI2016」所有公共子序列问题
题解 听说是什么序列自动机? 我们考虑对于每个位置的串,下面拼接相同的字符时,拼接最近的一个,这样可以保证不重不漏 为了实现这个我们需要什么呢,我们需要一个链表,记录一下每个位置的下一个字符会转移到哪 ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode 77: 最长公共子序列
public class Solution { /** * @param A, B: Two string. * @return: the length of the longest common s ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
随机推荐
- 聊聊 Laravel 5.5 的 「自动发现」
ThinkSNS是什么? ThinkSNS(简称TS),一款全平台综合性社交系统,目前最新版本为ThinkSNS+.ThinkSNS V4 ThinkSNS[简]. 看了Taylor Otwell发表 ...
- ps 命令参数解释
转自:https://www.cnblogs.com/fps2tao/p/7692482.html A 显示所有进程(等价于-e)(utility)-a 显示一个终端的所有进程,除了会话引线-N 忽略 ...
- 用jQuery开发插件详解
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- CC20:高度最小的BST
题目 对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树. 给定一个有序序列int[] vals,请返回创建的二叉查找树的高度. 解法 这道题感觉如果没有创建树的 ...
- mac mysql 编码配置
mac mysql 编码配置 (mysql目录下没有my.cnf) 想要修改编码发现自己的/usr/local/mysql/support-files里面根本没有my.cnf 安装方式是去mysql官 ...
- python数值类型与序列类型
基本运算符 / 浮点除法 //整除 x**y x的y次方 python中严格区分大小写 type(xx)/内置函数,查看变量xx的类型 id(xx)/内置函数,查看变量xx的内存地址 //----- ...
- springMVC框架的理解加深,个人的一些想法
一 写spring-boot整合的时候,有种想看源码的冲动!呸,是钻牛角尖的毛病犯了... @RequestMapping("/index") public String inde ...
- vue学习之路之需要了解的知识汇总
一.vue是什么? 相关网页: https://vuejs.bootcss.com/v2/guide/ 及菜鸟教程 https://www.runoob.com/vue2/v ...
- To the world you may be one person, but to one person you may be the world.
To the world you may be one person, but to one person you may be the world.对于世界而言,你是一个人:但对于某人而言,你是他的 ...
- Mind must be master of the body, strong mind can separate the body from its suffering.
Mind must be master of the body, strong mind can separate the body from its suffering.意志是身体的主人,有顽强的意 ...