UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213406#2068. 最长公共前缀erican11274ms78100kbC++113.0kb2024-11-11 21:39:112024-11-11 23:07:35

answer

// Problem: C. 最长公共前缀
// Contest: undefined - NOIP2024训练赛 02
// URL: http://noi.ac/contest/1154/problem/2068
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
// Challenger: Erica N
// ----
// 
#include<bits/stdc++.h>

using namespace std;
#define rd read()
#define ull unsigned long long
#define int long long 
#define pb push_back
#define itn int
#define ps second 
#define pf first


#define rd read()
int read(){
  int xx = 0, ff = 1;char ch = getchar();
  while (ch < '0' || ch > '9'){
    if (ch == '-')ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')xx = xx * 10 + (ch - '0'), ch = getchar();
  return xx * ff;
}
#define zerol = 1
#ifdef zerol
#define cdbg(x...) do { cerr << #x << " -> "; err(x); } while (0)
void err() {cerr << endl;}
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
    for (auto v: a) cerr << v << ' ';err(x...);
}
template<typename T, typename... A>
void err(T a, A... x) {
    cerr << a << ' ';err(x...);
}
#else
#define dbg(...)
#endif
const int N=4e3+5;
const ull P=137;
const int M=2001;
const int INF=1e18+7;
/*

策略


*/    


ull hs[N][N],ht[N][N];
ull pw[N],hl[N][N];
string c[N];
int n,m,q;

int tot;

ull getHs(int x,int y,char op,int len){
    // cdbg("gh",x,y,op);
    if(op=='H'){
        return hs[x][y+len-1]-hs[x][y-1]*pw[len];
    }
    if(op=='V'){
        return ht[x+len-1][y]-ht[x-1][y]*pw[len];
    }
    if(op=='D'){
        int id=x-y+M;
        return hl[id][x+len-1]-hl[id][x-1]*pw[len];
    }
    return 0;
}



bool check(int x,int y,char op,int len){
    // return 0;
    if(op=='H'){
        if(y+len-1>m)return 0;
    }
    if(op=='V'){
        if(x+len-1>n)return 0;
    }
    if(op=='D'){
        if(x+len-1>n)return 0;
        if(y+len-1>m)return 0;
    }
    return 1;
}
signed main(){
     n=rd,m=rd,q=rd;
    for(int i=1;i<=n;i++){
        cin>>c[i];
        c[i]=" "+c[i];
    }
    
    pw[0]=1;
    
    for(int i=1;i<=max(n,m);i++){
        pw[i]=pw[i-1]*P;
    }
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            hs[i][j]=hs[i][j-1]*P+c[i][j];
        }
    }
    
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            ht[j][i]=ht[j-1][i]*P+c[j][i];
        }
    }
    
    for(int k=1-m;k<=n-1;k++){
        for(int i=1;i<=n;i++){
            int j=i-k;
            if(j>m)break;
            if(j>0)hl[k+M][i]=hl[k+M][i-1]*P+c[i][j];
        }
    }
    
    // cdbg("OJK");
    
    while(q--){
        // tot++;
        int a=rd,b=rd;
        char c,z;
        cin>>c;
        int x=rd,y=rd;
        cin>>z;
    
        int l=1,r=INF;
        int res=0;
        while(l<=r){
            int mid=l+r>>1;
            if(check(a,b,c,mid)&&check(x,y,z,mid)&&getHs(a,b,c,mid)==getHs(x,y,z,mid))l=mid+1,res=mid;
            else r=mid-1;
        }
        
        cout<<res<<endl;
        // cdbg("OK",q);
        
        
    }
} 

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 11
Accepted

Test #1:

score: 11
Accepted
time: 138ms
memory: 70104kb

input:

1000 2000 10000
awanannwnnaaawwnwawanwnwaaaanwnaaananwawwwwnannannawwwawwaaaaannwnwnwnnaaawaawawannn...

output:

0
0
0
0
0
0
0
2
0
1
0
0
1
0
1
0
2
0
1
2
0
0
2
0
1
1
0
0
0
0
1
0
0
0
1
0
0
1
0
1
1
0
2
0
1
3
1
0
3
0
...

result:

ok 10000 tokens

Test #2:

score: 0
Accepted
time: 136ms
memory: 78100kb

input:

2000 1000 10000
zzqzqqqqqzqqqqzqqqqzqzqzzzqqzqqqzzqzzqqqqqqqqqqqzzqzqqqzzqqzzqzqzzzqqzqqzzzzzzqzzzqq...

output:

0
3
3
1
0
2
2
2
1
0
1
0
0
0
1
0
2
0
6
1
0
1
1
0
2
1
1
1
2
4
0
1
3
2
1
0
1
2
0
0
0
1
1
0
1
1
0
4
0
3
...

result:

ok 10000 tokens

Subtask #2:

score: 0
Time Limit Exceeded

Test #3:

score: 0
Time Limit Exceeded

input:

2000 2000 1000000
gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...

output:

1146
541
203
322
618
166
345
138
520
206
1031
667
741
921
361
1110
1057
372
899
209
491
69
93
639
14...

result:


Subtask #3:

score: 0
Skipped

Subtask #4:

score: 0
Skipped