大意: 给定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. pl/sql Developer连接oracle远程数据库

    1.下载地址:PLSQL Developer 12.1正式版64位(含中文语言包.注册码).rar   https://download.csdn.net/download/bokewangyu/11 ...

  2. DS-博客作业07

    1.本周学习总结(0--2分) 1.1思维导图 1.2 谈谈你对查找运算的认识及学习体会. 在查找这一章,我学习的比较认真,但是还是有部分没太清楚.这章没有前一章树那么多的代码要记,但是还是要用心. ...

  3. Nginx-HTTP之静态网页访问流程分析二

    11. HTTP 阶段执行 下面会依次执行以下阶段: NGX_HTTP_SERVER_REWRITE_PHASE: 在将请求的 URI 与 location 表达式匹配前,修改请求的 URI (所谓重 ...

  4. Remote Ubuntu VM from Windows

    Need to install the xrdp tool on Ubuntu. To do this, open a Terminal window (Ctrl + Alt + T) and ent ...

  5. decimal模块 --数字的精度、保留小数位数、取整问题

    开始之前需要注意一点是:精度值为数字的总位数,如:1.23, 精度值为3: 0.123,精度值也为3 1.更改默认精度值后,直接进行计算即可保留对应精度值 from decimal import ge ...

  6. bootstrp的datetimepicker插件获取选定日期

    碰到一个日期选择,并将日期存储到数据库的需求,需要利用bootstrp的datetimepicker插件获取选定日期,并将其转换为指定字符窜,简单记录下实现的过程. 1. datetimepicker ...

  7. PHP遍历目录下的文件夹和文件 以及遍历文件下内容

    1.遍历目录下的文件夹和文件: public function bianli1($dir) { $files = array(); if($head = opendir($dir)) { while( ...

  8. 阿里云Ubuntu 16 FTP安装配置注意事项

    1. 开放端口设置 阿里云控制台添加"安全组规则". 1) 21: FTP端口; 2) 15000~15100: 对应vsftpd.conf 自定义配置. (重要!) pasv_e ...

  9. 性能测试 | 记一次生产数据库sql由451s优化为0.4s的过程

    概述 最近开发说某个接口跑的很慢,排查了下发现其中一条sql,数据量不大,但居然要跑451s,下面简单记录一下优化的过程. 问题sql SELECT l.location_gid ENUMVALUE, ...

  10. LC 583. Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...