Loi Online Judge 42. 「Loi57 test 2017.4.22」字符串

题目描述

给定 n 个串,m次询问,每次询问两个串的 lcp (最长公共前缀)

输入格式

第一行包含两个数 n,m ,表示 n 个串,m次询问。

接下来 n 行,每行一个串。

再接下来m行,每行两个整数 a,b ,表示询问编号为 a b的两个串的 lcp

输出格式

输出包含 m 行,即为询问的答案

样例一

input

5 3
except
expert
expect
example
exam
1 2
2 3
4 5

output

2
4
4

限制与约定

对于30%的数据, m103, 每个串的长度 103 ;

对于 100% 的数据, n103,m105, 每个串的长度 104 .

时间限制: 1s

空间限制: 512MB

思路
裸哈希+二分答案
预处理每个字符串的每前i位的哈希值;
二分答案,若前i位哈希值一样,则调高答案;否则调低;

注意
strlen的时间复杂度是O(n),所以要预处理,O(1)调用,否则会超时;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
char c[1005][10005];
const unsigned long long  P=29;
unsigned long long  hs[1005][10005],len[1005];
int a,b,n,m;
using namespace std;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",c[i]);
        len[i]=strlen(c[i]);
        for(int j=0;j<len[i];j++){
            hs[i][j+1]=hs[i][j]*P+(c[i][j]-'a'+1);
        }
    }
    for(int j=1;j<=m;j++){
        scanf("%d%d",&a,&b);
        int l=0;
        int r=min(len[a],len[b])+1;
        while(r-l>1){
            int mid=(r+l)/2;
            if(hs[a][mid]==hs[b][mid]) l=mid;
                else r=mid;
            }
        printf("%d\n",l);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我地道的翻译:The differential variational inequalities ((DVIs), for short) are useful for the study of models involving both dynamics and constraints in the form of in￾equalities. They arise in many applications: electrical circuits with ideal diodes, Coulomb friction problems for contacting bodies, economical dynamics, dynamic traffic networks. Pang and Stewart [26], [27] established the existence, unique￾ness, and Lipschitz dependence of solutions subject to boundary conditions for (DVIs) in finite dimensional spaces. Han and Pang investigated a class of dif￾ferential quasi-variational inequalities in [11], and Li, Huang and O’Regan [18] studied a class of differential mixed variational inequalities in finite dimensional Well-Posedness of Differential Mixed Quasi-Variational-Inequalities 137 spaces. Gwinner [8] obtained an equivalence result between (DVIs) and projected dynamical systems. In [9] he also proved a stability property for (DVIs) by using the monotonicity method of Browder and Minty, and Mosco set convergence. Chen and Wang [4] studied dynamic Nash equilibrium problems which have the formulation of differential mixed quasi-variational inequalities. Elastoplastic contact problems can also be incorporated into (DMQVIs) formulation because general dynamic processes in the nonsmooth unilateral contact problems are governed by quasi-variational inequalities. A numerical study for nonsmooth contact problems with Tresca friction can be found in [10], Liu, Loi and Obukhovskii [19] studied the existence and global bifurcation for periodic solutions of a class of (DVIs) by using the topological degree theory for multivalued maps and the method of guiding functions. For more details about (DVIs) we refer to [3], [30], [12], [22]–[21].
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值