Mysterious Crime CodeForces - 1043D (哈希)
大意: 给定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 (哈希)的更多相关文章
- Mysterious Crime CodeForces - 1043D (思维+组合数学)
Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...
- CodeForces 1043D Mysterious Crime 区间合并
题目传送门 题目大意: 给出m个1-n的全排列,问这m个全排列中有几个公共子串. 思路: 首先单个的数字先计算到答案中,有n个. 然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个 ...
- [题解]Codeforces Round #519 - D. Mysterious Crime
[题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...
- 【Codeforces Round #519 by Botan Investments D】Mysterious Crime
[链接] 我是链接,点我呀:) [题意] 相当于问你这m个数组的任意长度公共子串的个数 [题解] 枚举第1个数组以i为起点的子串. 假设i..j是以i开头的子串能匹配的最长的长度. (这个j可以给2. ...
- Codeforces Round #519 D - Mysterious Crime
题目 题意: 在m组数,每组有n个数(数的范围1-n)中,找到某些序列 使它是每组数的一个公共子序列,问这样的某些序列的个数? 思路: 不难想出答案ans是≥n的. 创立一个next数组,使每组中第i ...
- D. Mysterious Crime
链接 [http://codeforces.com/contest/1043/problem/D] 题意 给你一个m*n的矩阵(m<=10,n<=1e5), 每一行的数字是1到n里不同的数 ...
- CF1043D Mysterious Crime
思路: 参考了http://codeforces.com/blog/entry/62797,把第一个序列重标号成1,2,3,...,n,在剩下的序列中寻找形如x, x + 1, x + 2, ...的 ...
- cf1043E. Mysterious Crime(二分 前缀和)
题意 题目链接 Sol 考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了. 看了看E发现好像很可做?? 又仔细看了几眼发现这不是sb题么... 先考虑两个人,假设贡献分别为\((x, y ...
- cf1043D. Mysterious Crime(枚举)
题意 题目链接 给出\(m\)个长度为\(n\)的排列,问有多少连续公共子串 \(m \leqslant 10, n \leqslant 10^5\) Sol 非常naive的一道题然而交了3遍才过( ...
随机推荐
- 【redis 学习系列】API的理解与使用(三)
4.列表 列表(list)类型是用来存储多个有序的字符串,如图2-18所示,a.b.c.d.e五个元素从左到右组成一个有序列表,列表中的每个字符串称为元素,一个列表最大可以存储2^32-1个元素.在R ...
- solr系列之solr-5.5.5 window单机版默认Jetty安装
Solr5.5.5单机部署 Solr5和Solr4有很大区别,最为明显的就是Solr5已经可以独立部署,从Solr5开始,Solr已经不再以war包形式部署,Solr已经成为了一个独立的java服务端 ...
- centos7下面安装tomcat
前言 对于一个新安装的 centos 系统来说,是没有 tomcat 服务器的.用下面的命令可以查看 tomcat 服务的状态. systemctl status tomcat.service//或者 ...
- Job for docker.service failed because the control process exited with error code. See
在Liunx中使用Docker, 注: Liunx使用的是在虚拟机下的centOS7版本在刚开始安装Docker时没有任何错误, 但是在后续的docker启动过程中, 出现以下问题: Job for ...
- js中几种动态创建元素并设置文本内容的比较,及性能测试。
内容 1 appendChild (都兼容) 2.insertAdjacentHTML (都兼容) 3.innerHTML (都兼容) 4.createDocumentFragment (都兼容) 动 ...
- kotlin if
val max = if (a > b) { print("Choose a") a } else { print("Choose b") b } 注意和 ...
- Win10蓝牙鼠标老是断连卡顿的解决方法
一直用一个微软家的蓝牙鼠标,饱受鼠标卡顿困扰,今天找到了一个解决方案,用了下,效果显著.具体操作见下文. 原文地址:https://jingyan.baidu.com/article/c85b7a64 ...
- AutoMapUtility
实体属性自动映射,支持对象间属性值复制和List复制 安装 Install-Package AutoMapUtility -Version 1.0.2 https://github.com/leopa ...
- 基于SAR对Linux资源的监控shell脚本
#! /bin/bash ] # $# 传递给脚本或函数的参数个数 then 脚本名称 exit -; fi SLEEP_TIME=$ LOG=$ while true do #线程数 thread_ ...
- Mac 配置vscode调试PHP
Mac系统版本:MacOS Mojave 10.14.5 vscode:1.36.0 MacOS Mojave 10.14.5 系统自带 PHP 7.1.23 1.开启php sudo vim / ...