QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#523943#4681. Keyboardingucup-team3474AC ✓1358ms96800kbC++203.9kb2024-08-19 00:29:512024-08-19 00:29:51

Judging History

你现在查看的是最新测评结果

  • [2024-08-19 00:29:51]
  • 评测
  • 测评结果:AC
  • 用时:1358ms
  • 内存:96800kb
  • [2024-08-19 00:29:51]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1919810;
typedef int ll;
typedef pair<ll,ll> PII;
ll n,m,k;
ll a[N],b[N];
char s[N];
double val[N];
string ss[N];
int dp[55][55];
int last[55][55];

vector<PII> e[N];
vector<int> ed[N];
vector<PII> lst,now;

int dist[2550][2550];
bool tf[2550];





void bfs(int d[],int x){
    memset(tf,false,sizeof tf);
    d[x]=0;
    tf[x]=1;
    queue<int> q;
    q.push(x);
    while(q.size()){
        int t=q.front();
        // cout<<t<<endl;
        q.pop();
        for(auto j:ed[t]){
            if(tf[j]) continue;
            d[j]=d[t]+1;
            q.push(j);
            tf[j]=1;
        }
    }
}


void __(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>ss[i];
    }
    scanf("%s",s+1);
    memset(dp,0x3f,sizeof dp);
    memset(last,0x3f,sizeof last);
    memset(dist,0x3f,sizeof dist);
    for(int i=0;i<n;i++){
        int now=ss[i][0],bg=0,ls=-1;

        for(int j=0;j<m;j++){
            if(ss[i][j]!=ss[i][bg]){
                ls=j-1;
                bg=j;
            }
            if(ls!=-1){
                ed[i*m+j].push_back(i*m+ls);
            }
        }
        bg=m-1,ls=-1;
        for(int j=m-1;j>=0;j--){
            if(ss[i][j]!=ss[i][bg]){
                ls=j+1;
                bg=j;
            }
            if(ls!=-1){
                ed[i*m+j].push_back(i*m+ls);
            }
        }
    }
    for(int i=0;i<m;i++){
        int now=ss[i][0],bg=0,ls=-1;

        for(int j=0;j<n;j++){
            if(ss[j][i]!=ss[bg][i]){
                ls=j-1;
                bg=j;
            }
            if(ls!=-1){
                ed[j*m+i].push_back(ls*m+i);
            }
        }

        bg=n-1,ls=-1;
        for(int j=n-1;j>=0;j--){
            if(ss[j][i]!=ss[bg][i]){
                ls=j+1;
                bg=j;
            }
            if(ls!=-1){
                ed[j*m+i].push_back(ls*m+i);
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            bfs(dist[i*m+j],i*m+j);
        }
    }


    last[0][0]=0;
    int len=strlen(s+1);
    lst.push_back({0,0});
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int t=ss[i][j];
            e[t].push_back({i,j});
        }
    }
    s[len+1]='*';
    for(int i=1;i<=len+1;i++){
        int t=s[i];
        now=e[t];

        priority_queue<PII,vector<PII>,greater<PII>> q;
        for(auto [x,y]:lst){
            q.push({last[x][y],x*m+y});
            dp[x][y]=last[x][y];
        }
        
        memset(tf,false,sizeof tf);
        while(q.size()){
            int t=q.top().second;
            ll dd=q.top().first;
            // cout<<dd<<" "<<t<<endl;
            q.pop();
            if(tf[t]) continue;
            tf[t]=1;
            
            for(auto x:ed[t]){
                int r=x/m,c=x%m;
                // cout<<r<<" "<<c<<endl;
                if(dp[r][c]>dd+1){
                    dp[r][c]=dd+1;
                    // int x=r*m+c;
                    if(!tf[x]) q.push({dp[r][c],x});
                }
            }
        }
        // for(auto [x,y]:now){
        //     for(auto [u,v]:lst){

        //         dp[x][y]=min(dp[x][y],last[u][v]+dist[u*m+v][x*m+y]);
        //     }
        // }
        for(int j=0;j<n;j++){
            for(int k=0;k<m;k++){
                last[j][k]=0x3f3f3f3f;
                if(ss[j][k]==s[i])
                last[j][k]=dp[j][k];
                dp[j][k]=0x3f3f3f3f;
                // cout<<last[j][k]<<" ";
            }
            // cout<<endl;
        }
        // cout<<endl;
        lst=now;
        now.clear();
    }
    int ans=0x3f3f3f3f;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            ans=min(ans,last[i][j]);
        }
    }
    printf("%d\n",ans+len+1);
}


int main(){
    int _=1;
    // cin>>_;
    while(_--){
        __();
    }
}

详细

Test #1:

score: 100
Accepted
time: 7ms
memory: 94424kb

input:

4 7
ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ**
CONTEST

output:

30

result:

ok 1 number(s): "30"

Test #2:

score: 0
Accepted
time: 19ms
memory: 95008kb

input:

5 20
12233445566778899000
QQWWEERRTTYYUUIIOOPP
-AASSDDFFGGHHJJKKLL*
--ZZXXCCVVBBNNMM--**
--------------------
ACM-ICPC-WORLD-FINALS-2015

output:

160

result:

ok 1 number(s): "160"

Test #3:

score: 0
Accepted
time: 7ms
memory: 94952kb

input:

2 19
ABCDEFGHIJKLMNOPQZY
X*****************Y
AZAZ

output:

19

result:

ok 1 number(s): "19"

Test #4:

score: 0
Accepted
time: 11ms
memory: 93688kb

input:

6 4
AXYB
BBBB
KLMB
OPQB
DEFB
GHI*
AB

output:

7

result:

ok 1 number(s): "7"

Test #5:

score: 0
Accepted
time: 8ms
memory: 94944kb

input:

4 3
XYZ
AAA
CAD
B**
A

output:

5

result:

ok 1 number(s): "5"

Test #6:

score: 0
Accepted
time: 15ms
memory: 94668kb

input:

1 2
A*
A

output:

3

result:

ok 1 number(s): "3"

Test #7:

score: 0
Accepted
time: 8ms
memory: 96800kb

input:

2 1
*
A
A

output:

4

result:

ok 1 number(s): "4"

Test #8:

score: 0
Accepted
time: 8ms
memory: 93536kb

input:

12 11
AAAAAAAAAAA
ABBBBBBBBBA
ABCCCCCCCBA
ABCDDDDDCBA
ABCDEEEDCBA
ABCDEFEDCBA
ABCDEEEDCBA
ABCDDDDDCBA
ABCCCCCCCBA
ABBBBBBBBBA
AAAAAAAAAAA
***********
AAA

output:

5

result:

ok 1 number(s): "5"

Test #9:

score: 0
Accepted
time: 77ms
memory: 93800kb

input:

6 28
AABBCCDDEEFFGGHHIIJJKKLLMNNN
AABBCCDDEEFFGGHHIIJJKKLLMMMN
OOPPQQRRSSTTUUVVWWXXYYZZ00**
OOPPQQRRSSTTUUVVWWXXYYZZ0011
222333444555666777888999--11
222333444555666777888999----
1THE-QUICK-BROWN-FOX-JUMPS-OVER-THE-LAZY-DOG2THE-QUICK-BROWN-FOX-JUMPS-OVER-THE-LAZY-DOG3THE-QUICK-BROWN-FOX-JUMPS-OVER-T...

output:

64174

result:

ok 1 number(s): "64174"

Test #10:

score: 0
Accepted
time: 28ms
memory: 94264kb

input:

30 30
A11111111111111111111111111111
0B1111111111111111111111111111
00C111111111111111111111111111
000D11111111111111111111111111
0000E1111111111111111111111111
00000F111111111111111111111111
000000G11111111111111111111111
0000000H1111111111111111111111
00000000I111111111111111111111
000000000J11111...

output:

590057

result:

ok 1 number(s): "590057"

Test #11:

score: 0
Accepted
time: 166ms
memory: 94900kb

input:

30 30
A11111111111111111111111111111
0B1111111111111111111111111111
00C111111111111111111111111111
000D11111111111111111111111111
0000E1111111111111111111111111
00000F111111111111111111111111
000000G11111111111111111111111
0000000H1111111111111111111111
00000000I111111111111111111111
000000000J11111...

output:

30001

result:

ok 1 number(s): "30001"

Test #12:

score: 0
Accepted
time: 13ms
memory: 93356kb

input:

4 10
QWERTYUIOP
ASDFGHJKL*
ZXCVBNM***
----------
GA-NU-MAAR-LIGGEN-LIEFSTE-IN-DE-TUIN-DE-LEGE-PLEKKEN-IN-HET-HOGE-GRAS-IK-HEB-ALTIJD-GEWILD-DAT-IK-DAT-WAS-EEN-LEGE-PLEK-VOOR-IEMAND-OM-TE-BLIJVEN

output:

676

result:

ok 1 number(s): "676"

Test #13:

score: 0
Accepted
time: 19ms
memory: 93604kb

input:

24 30
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
AAASSSDDDFFFGGGHHHJJJKKKLLL***
AAASSSDDDFFFGGGHHHJJJKKKLLL***
AAASSSDDDFFFGGGHHHJJJKKKLLL***
AAASSSDDDFFFGGG...

output:

2236

result:

ok 1 number(s): "2236"

Test #14:

score: 0
Accepted
time: 1358ms
memory: 94664kb

input:

50 50
*0000000000000000000000000000000000000000000000000
00011001100110011001100110011001100110011001100111
01001100110011001100110011001100110011001100110011
01100110011001100110011001100110011001100110011001
00110011001100110011001100110011001100110011001101
000110011001100110011001100110011001100...

output:

20003

result:

ok 1 number(s): "20003"

Test #15:

score: 0
Accepted
time: 1331ms
memory: 94484kb

input:

50 50
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABA
ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBABA
ABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABABA
ABABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

20001

result:

ok 1 number(s): "20001"

Test #16:

score: 0
Accepted
time: 41ms
memory: 94584kb

input:

50 50
0*************************************************
--************************************************
---***********************************************
----**********************************************
-----*********************************************
------*********************************...

output:

979905

result:

ok 1 number(s): "979905"

Test #17:

score: 0
Accepted
time: 1195ms
memory: 93988kb

input:

50 50
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
*AAABABBBABAAABABBBABAAABABBBABAAABABBBABAAABABBBA
BAAABABBBABAAABABBBABAAABABBBABAAABABBBABAAABAB0BA
BAAABABBBABAAABABBBABAAABABBBABAAABABBBABAAABABBBA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

output:

1650040

result:

ok 1 number(s): "1650040"

Test #18:

score: 0
Accepted
time: 1233ms
memory: 94188kb

input:

50 50
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
4AAABABBBABAAABABBBABABABABABABAAABABBBABAAABABBBA
BAAABABBBABAAABABBBABABABABABABAAABABBBABAAABAB0BA
BAAABABBBABAAABABBBABABABABABABAAABABBBABAAABABBBA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

output:

1810204

result:

ok 1 number(s): "1810204"

Test #19:

score: 0
Accepted
time: 1103ms
memory: 94296kb

input:

50 50
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBAAABABBBABAAABABBBABAABABABABAAABABBBABAAABABBBA
0BAAABABBBABAAABABBBABAABABABABAAABABBBABAAABAB4BA
BBAAABABBBABAAABABBBABAABABABABAAABABBBABAAABABBBA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

3270317

result:

ok 1 number(s): "3270317"