Submission #69685779
Source Code Expand
import sys
from collections import defaultdict
MOD=998244353
input=sys.stdin.readline
N=int(input().strip())
T=input().strip()
A=[ord(c)-97 for c in T]
if len(set(A))==N:
x=1
for i in range(2,N+1):
x=x*i%MOD
print(x)
sys.exit(0)
Nx=[[-1]*26 for _ in range(N+1)]
last=[-1]*26
for pos in range(N-1,-1,-1):
last[A[pos]]=pos
row=Nx[pos]
for c in range(26):
row[c]=last[c]
P=[[-1]*26 for _ in range(N+1)]
prev=[-1]*26
for pos in range(N+1):
row=P[pos]
for c in range(26):
row[c]=prev[c]
if pos<N:
prev[A[pos]]=pos
pref=[0]*(N+1)
for i in range(N):
pref[i+1]=pref[i]|(1<<A[i])
def mask_between(l,r):
return pref[r]&~pref[l]
cur={():1}
for _ in range(N):
nxt=defaultdict(int)
for s,cnt in cur.items():
k=len(s)
L=[-1]*(k+1)
x=-1
for i in range(k):
x=Nx[x+1][s[i]]
L[i+1]=x
R=[N]*(k+1)
x=N
for i in range(k-1,-1,-1):
x=P[x][s[i]]
R[i]=x
seen=set()
for r in range(k+1):
lb=L[r]
rb=R[r]
if rb-lb<=1:
continue
m=mask_between(lb+1,rb)
while m>=0:
b=m&-m
m-=b
ch=(b.bit_length()-1)
s2=s[:r]+(ch,)+s[r:]
if s2 in seen:
continue
seen.add(s2)
nxt[s2]=(nxt[s2]+cnt)%MOD
cur=nxt
print(cur.get(tuple(A),0)%MOD)
Submission Info
| Submission Time | |
|---|---|
| Task | F - Inserting Process |
| User | uparupaaa |
| Language | Python (PyPy 3.10-v7.3.12) |
| Score | 0 |
| Code Size | 1370 Byte |
| Status | TLE |
| Exec Time | 2765 ms |
| Memory | 83052 KiB |
Judge Result
| Set Name | Sample | All | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 500 | ||||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | TLE | 2762 ms | 82324 KiB |
| 00_sample_01.txt | AC | 70 ms | 76804 KiB |
| 00_sample_02.txt | TLE | 2762 ms | 82932 KiB |
| 01_test_00.txt | AC | 70 ms | 76760 KiB |
| 01_test_01.txt | TLE | 2762 ms | 82980 KiB |
| 01_test_02.txt | TLE | 2765 ms | 82636 KiB |
| 01_test_03.txt | AC | 70 ms | 76904 KiB |
| 01_test_04.txt | TLE | 2762 ms | 83052 KiB |
| 01_test_05.txt | TLE | 2762 ms | 83048 KiB |
| 01_test_06.txt | TLE | 2762 ms | 82696 KiB |
| 01_test_07.txt | TLE | 2762 ms | 82928 KiB |
| 01_test_08.txt | TLE | 2762 ms | 83004 KiB |
| 01_test_09.txt | TLE | 2762 ms | 82884 KiB |
| 01_test_10.txt | TLE | 2763 ms | 82728 KiB |
| 01_test_11.txt | TLE | 2762 ms | 82580 KiB |
| 01_test_12.txt | TLE | 2762 ms | 82596 KiB |
| 01_test_13.txt | TLE | 2762 ms | 82600 KiB |
| 01_test_14.txt | TLE | 2763 ms | 82352 KiB |
| 01_test_15.txt | TLE | 2762 ms | 83000 KiB |
| 01_test_16.txt | TLE | 2762 ms | 82348 KiB |
| 01_test_17.txt | TLE | 2762 ms | 83052 KiB |
| 01_test_18.txt | AC | 70 ms | 76576 KiB |
| 01_test_19.txt | TLE | 2762 ms | 82580 KiB |
| 01_test_20.txt | TLE | 2763 ms | 82588 KiB |
| 01_test_21.txt | TLE | 2762 ms | 82368 KiB |
| 01_test_22.txt | TLE | 2763 ms | 82704 KiB |
| 01_test_23.txt | TLE | 2763 ms | 82724 KiB |
| 01_test_24.txt | TLE | 2762 ms | 82632 KiB |
| 01_test_25.txt | AC | 70 ms | 76824 KiB |