QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#523900 | #4681. Keyboarding | ucup-team3474 | TL | 3783ms | 96748kb | C++20 | 3.2kb | 2024-08-18 22:14:15 | 2024-08-18 22:14:16 |
Judging History
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];
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]);
}
}
lst.clear();
for(int j=0;j<n;j++){
for(int k=0;k<m;k++){
last[j][k]=dp[j][k];
dp[j][k]=0x3f3f3f3f;
if(last[j][k]<1e9) lst.push_back({j,k});
// 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: 8ms
memory: 93620kb
input:
4 7 ABCDEFG HIJKLMN OPQRSTU VWXYZ** CONTEST
output:
30
result:
ok 1 number(s): "30"
Test #2:
score: 0
Accepted
time: 11ms
memory: 93640kb
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: 11ms
memory: 94416kb
input:
2 19 ABCDEFGHIJKLMNOPQZY X*****************Y AZAZ
output:
19
result:
ok 1 number(s): "19"
Test #4:
score: 0
Accepted
time: 9ms
memory: 96700kb
input:
6 4 AXYB BBBB KLMB OPQB DEFB GHI* AB
output:
7
result:
ok 1 number(s): "7"
Test #5:
score: 0
Accepted
time: 18ms
memory: 93984kb
input:
4 3 XYZ AAA CAD B** A
output:
5
result:
ok 1 number(s): "5"
Test #6:
score: 0
Accepted
time: 8ms
memory: 94128kb
input:
1 2 A* A
output:
3
result:
ok 1 number(s): "3"
Test #7:
score: 0
Accepted
time: 7ms
memory: 93228kb
input:
2 1 * A A
output:
4
result:
ok 1 number(s): "4"
Test #8:
score: 0
Accepted
time: 8ms
memory: 94636kb
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: 12ms
memory: 94108kb
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: 23ms
memory: 94280kb
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: 3783ms
memory: 96748kb
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: 17ms
memory: 95124kb
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: 16ms
memory: 93944kb
input:
24 30 QQQWWWEEERRRTTTYYYUUUIIIOOOPPP QQQWWWEEERRRTTTYYYUUUIIIOOOPPP QQQWWWEEERRRTTTYYYUUUIIIOOOPPP QQQWWWEEERRRTTTYYYUUUIIIOOOPPP QQQWWWEEERRRTTTYYYUUUIIIOOOPPP QQQWWWEEERRRTTTYYYUUUIIIOOOPPP AAASSSDDDFFFGGGHHHJJJKKKLLL*** AAASSSDDDFFFGGGHHHJJJKKKLLL*** AAASSSDDDFFFGGGHHHJJJKKKLLL*** AAASSSDDDFFFGGG...
output:
2236
result:
ok 1 number(s): "2236"
Test #14:
score: -100
Time Limit Exceeded
input:
50 50 *0000000000000000000000000000000000000000000000000 00011001100110011001100110011001100110011001100111 01001100110011001100110011001100110011001100110011 01100110011001100110011001100110011001100110011001 00110011001100110011001100110011001100110011001101 000110011001100110011001100110011001100...