Submission #63092314


Source Code Expand

#include<bits/stdc++.h>
#define ll long long
#define int ll
#define L x<<1
#define R x<<1|1
#define mid ((l+r)>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define Root 1,1,n
#define OK Ll<=l&&r<=Rr
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
#define yy puts("Yes"),exit(0)
#define nn puts("No"),exit(0)
#define YY return puts("Yes"),void()
#define	NN return puts("No"),void()
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N=1e2+5,M=1e6+5,inf=(1LL<<30)-1,mod=998244353;
const ll llf=1e18;
int n,m;
struct node{
	int y,w;
};
vector<node>p[N];
vector<int>Q[N][27];
inline void add_(int a,int b,char c){
	Q[a][c-'a'+1].pb(b);
	p[b].pb({a,c-'a'+1});
}
string s[N];
int f[N][N],g[N][N][27];
struct nod{
	int x,y,c;
};
inline void bfs(){
	queue<nod>q;
	repn(x)f[x][x]=0,q.push({x,x,-1});
	repn(x)rep(c,1,26)for(auto y:Q[x][c])if(y^x)f[x][y]=1,q.push({x,y,-1});
	while(!q.empty()){
		int A=q.front().x,B=q.front().y,c=q.front().c;
		q.pop();
		if(c==-1){
			for(auto z:p[A]){
				int Z=z.y,C=z.w;
				if(g[Z][B][C]>f[A][B]+1)g[Z][B][C]=f[A][B]+1,q.push({Z,B,C});
			}
		}else {
			for(auto Y:Q[B][c]){
				if(f[A][Y]>g[A][B][c]+1)f[A][Y]=g[A][B][c]+1,q.push({A,Y,-1});
			}
		}
	}
}
inline void Main(){
	n=read();
	repn(i)cin>>s[i],s[i]='#'+s[i];
	repn(i)repn(j){
		if(s[i][j]!='-')add_(i,j,s[i][j]);
	}
	repn(i)repn(j)f[i][j]=inf;
	repn(i)repn(j)rep(c,1,26)g[i][j][c]=inf;
	bfs();
	repn(x){
		repn(y)if(f[x][y]==inf)cout <<-1<<' ';
		else cout <<f[x][y]<<' ';
		puts("");
	}
}
signed main(){
	int T=1;
	while(T--)Main();
	return 0;
}

Submission Info

Submission Time
Task E - Palindromic Shortest Path
User kkk_redbag_1314
Language C++ 20 (gcc 12.2)
Score 450
Code Size 2089 Byte
Status AC
Exec Time 14 ms
Memory 12236 KiB

Judge Result

Set Name Sample All After Contest
Score / Max Score 0 / 0 450 / 450 0 / 0
Status
AC × 2
AC × 58
AC × 2
Set Name Test Cases
Sample sample00.txt, sample01.txt
All sample00.txt, sample01.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt, testcase52.txt, testcase53.txt, testcase54.txt, testcase55.txt
After Contest after_contest00.txt, after_contest01.txt
Case Name Status Exec Time Memory
after_contest00.txt AC 5 ms 6480 KiB
after_contest01.txt AC 5 ms 6624 KiB
sample00.txt AC 1 ms 3548 KiB
sample01.txt AC 1 ms 3544 KiB
testcase00.txt AC 1 ms 3440 KiB
testcase01.txt AC 1 ms 3560 KiB
testcase02.txt AC 2 ms 4436 KiB
testcase03.txt AC 14 ms 12236 KiB
testcase04.txt AC 1 ms 3920 KiB
testcase05.txt AC 2 ms 5872 KiB
testcase06.txt AC 2 ms 5924 KiB
testcase07.txt AC 2 ms 5888 KiB
testcase08.txt AC 2 ms 5276 KiB
testcase09.txt AC 2 ms 6076 KiB
testcase10.txt AC 2 ms 4680 KiB
testcase11.txt AC 3 ms 5988 KiB
testcase12.txt AC 2 ms 5656 KiB
testcase13.txt AC 3 ms 5984 KiB
testcase14.txt AC 2 ms 4664 KiB
testcase15.txt AC 3 ms 5984 KiB
testcase16.txt AC 2 ms 4428 KiB
testcase17.txt AC 3 ms 5908 KiB
testcase18.txt AC 2 ms 5228 KiB
testcase19.txt AC 2 ms 6052 KiB
testcase20.txt AC 1 ms 4480 KiB
testcase21.txt AC 3 ms 6212 KiB
testcase22.txt AC 3 ms 5528 KiB
testcase23.txt AC 4 ms 6352 KiB
testcase24.txt AC 2 ms 4416 KiB
testcase25.txt AC 4 ms 6232 KiB
testcase26.txt AC 2 ms 4768 KiB
testcase27.txt AC 4 ms 6296 KiB
testcase28.txt AC 3 ms 5432 KiB
testcase29.txt AC 4 ms 6512 KiB
testcase30.txt AC 2 ms 5024 KiB
testcase31.txt AC 5 ms 6648 KiB
testcase32.txt AC 3 ms 5224 KiB
testcase33.txt AC 4 ms 6248 KiB
testcase34.txt AC 2 ms 4740 KiB
testcase35.txt AC 5 ms 6532 KiB
testcase36.txt AC 3 ms 5404 KiB
testcase37.txt AC 5 ms 6808 KiB
testcase38.txt AC 3 ms 5588 KiB
testcase39.txt AC 5 ms 6604 KiB
testcase40.txt AC 5 ms 6372 KiB
testcase41.txt AC 5 ms 6648 KiB
testcase42.txt AC 3 ms 5160 KiB
testcase43.txt AC 5 ms 6908 KiB
testcase44.txt AC 3 ms 5780 KiB
testcase45.txt AC 3 ms 6116 KiB
testcase46.txt AC 2 ms 4976 KiB
testcase47.txt AC 5 ms 6800 KiB
testcase48.txt AC 5 ms 6760 KiB
testcase49.txt AC 7 ms 7844 KiB
testcase50.txt AC 4 ms 6388 KiB
testcase51.txt AC 8 ms 8024 KiB
testcase52.txt AC 2 ms 5004 KiB
testcase53.txt AC 8 ms 8496 KiB
testcase54.txt AC 7 ms 7420 KiB
testcase55.txt AC 9 ms 8336 KiB