大意: 给定m个n排列, 求有多少个公共子串.

枚举每个位置, hash求出最大匹配长度.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e5+10;
int n, m, lst, s[12][N];
int pos[12][N];
int bas[N], has[12][N];
int Hash(int id, int l, int r) {
int ret = (has[id][r]-(ll)has[id][l-1]*bas[r-l+1])%P;
if (ret<0) ret += P;
return ret;
}
int calc(int x, int p1, int y, int p2) {
int r=min({lst+1,p1,p2});
return Hash(x,p1-r+1,p1)==Hash(y,p2-r+1,p2)?r:1;
}
int main() {
scanf("%d%d", &n, &m);
bas[0] = 1, bas[1] = n*20+1;
REP(i,2,n) bas[i]=(ll)bas[i-1]*bas[1]%P;
REP(i,1,m) REP(j,1,n) {
scanf("%d", s[i]+j);
has[i][j] = ((ll)has[i][j-1]*bas[1]+s[i][j])%P;
pos[i][s[i][j]] = j;
}
if (m==1) return printf("%lld\n",(ll)n*(n+1)/2),0;
ll ans = 0;
REP(i,1,n) {
int d = 1e9;
REP(j,2,m) d = min(d, calc(1,i,j,pos[j][s[1][i]]));
ans += d;
lst = d;
}
printf("%lld\n", ans);
}

Mysterious Crime CodeForces - 1043D (哈希)的更多相关文章

  1. Mysterious Crime CodeForces - 1043D (思维+组合数学)

    Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...

  2. CodeForces 1043D Mysterious Crime 区间合并

    题目传送门 题目大意: 给出m个1-n的全排列,问这m个全排列中有几个公共子串. 思路: 首先单个的数字先计算到答案中,有n个. 然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个 ...

  3. [题解]Codeforces Round #519 - D. Mysterious Crime

    [题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...

  4. 【Codeforces Round #519 by Botan Investments D】Mysterious Crime

    [链接] 我是链接,点我呀:) [题意] 相当于问你这m个数组的任意长度公共子串的个数 [题解] 枚举第1个数组以i为起点的子串. 假设i..j是以i开头的子串能匹配的最长的长度. (这个j可以给2. ...

  5. Codeforces Round #519 D - Mysterious Crime

    题目 题意: 在m组数,每组有n个数(数的范围1-n)中,找到某些序列 使它是每组数的一个公共子序列,问这样的某些序列的个数? 思路: 不难想出答案ans是≥n的. 创立一个next数组,使每组中第i ...

  6. D. Mysterious Crime

    链接 [http://codeforces.com/contest/1043/problem/D] 题意 给你一个m*n的矩阵(m<=10,n<=1e5), 每一行的数字是1到n里不同的数 ...

  7. CF1043D Mysterious Crime

    思路: 参考了http://codeforces.com/blog/entry/62797,把第一个序列重标号成1,2,3,...,n,在剩下的序列中寻找形如x, x + 1, x + 2, ...的 ...

  8. cf1043E. Mysterious Crime(二分 前缀和)

    题意 题目链接 Sol 考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了. 看了看E发现好像很可做?? 又仔细看了几眼发现这不是sb题么... 先考虑两个人,假设贡献分别为\((x, y ...

  9. cf1043D. Mysterious Crime(枚举)

    题意 题目链接 给出\(m\)个长度为\(n\)的排列,问有多少连续公共子串 \(m \leqslant 10, n \leqslant 10^5\) Sol 非常naive的一道题然而交了3遍才过( ...

随机推荐

  1. 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”

    在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...

  2. Flutter常见数据类型及数据类型转换

    简介 既然 Dart 是一门语言,那么就和我们所知道语言一样,有基本的数据类型以及常见的流程处理语法,那么我们就来了解下. Dart 的所有东西都是对象,包括数字,函数等.它们都继承自 Object ...

  3. 黑马vue---20、v-if和v-show的使用和特点

    黑马vue---20.v-if和v-show的使用和特点 一.总结 一句话总结: v-if 的特点:每次都会重新删除或创建元素 v-show 的特点: 每次不会重新进行DOM的删除和创建操作,只是切换 ...

  4. postman设置环境变量,实现一套接口根据选择的环境去请求不同的url

    一个系统,有本地,开发,测试,生产等不同的环境,如果写不同的url配置多套会比较麻烦,可以设置不同的环境实现不同的url之间的切换.配置之后如下: 第一步: 第二步: 添加环境变量 ps::不同的环境 ...

  5. MySQL 中<=>用法(长知识)

    https://www.runoob.com/mysql/mysql-operator.html MySQL 运算符 本章节我们主要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下 ...

  6. OpenCV中出现“Microsoft C++ 异常: cv::Exception,位于内存位置 0x0000005C8ECFFA80 处。”的异常

    对于OpenCV的安装 要感谢网友空晴拜小白提供的教程 链接如下: https://blog.csdn.net/sinat_36264666/article/details/73135823?ref= ...

  7. Ansible批量自动化管理工具 roles标准化

    批量管理服务器的工具,无需部署代理,通过ssh进行管理,是python写的 ansible 常用模块 : (1)shell命令模块执行命令 (2)copy模块批量下发文件或文件夹 (3)script模 ...

  8. smarty section 循环不同的四个样式

    <div class="moban_spzs"> <{section name=goodslist loop=$strdata6}> <{if $sm ...

  9. pycharm建立django工程

    1.windows上安装了python 用pycharm建立django工程,必须要连接本地的python,也就是windows上的pyhton,不能连接linux上的pyhton,否则报错:plea ...

  10. GB、GBDT、XGboost理解

    GBDT和xgboost在竞赛和工业界使用都非常频繁,能有效的应用到分类.回归.排序问题,虽然使用起来不难,但是要能完整的理解还是有一点麻烦的.本文尝试一步一步梳理GB.GBDT.xgboost,它们 ...