結果
| 問題 |
No.1646 Avoid Palindrome
|
| コンテスト | |
| ユーザー |
merlin
|
| 提出日時 | 2021-08-13 22:13:31 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,639 ms / 3,000 ms |
| コード長 | 1,698 bytes |
| コンパイル時間 | 4,427 ms |
| コンパイル使用メモリ | 77,964 KB |
| 実行使用メモリ | 41,352 KB |
| 最終ジャッジ日時 | 2024-11-08 15:31:39 |
| 合計ジャッジ時間 | 45,143 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
import java.io.*;
import java.util.*;
class Main
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
char s[]=bu.readLine().toCharArray();
int i,cur=1;
long dp[][][]=new long[2][26][26],sum[]=new long[26],M=998244353;
if(n==1)
{
int ans=1;
if(s[0]=='?') ans=26;
System.out.println(ans);
return;
}
if(n>1)
{
int j,v,v2;
if(s[1]=='?') v=(1<<26)-1;
else v=1<<(s[1]-'a');
if(s[0]=='?') v2=(1<<26)-1;
else v2=1<<(s[0]-'a');
for(i=0;i<26;i++)
if(((v>>i)&1)==1)
for(j=0;j<26;j++)
if(j!=i && ((v2>>j)&1)==1) dp[0][i][j]++;
}
for(i=2;i<n;i++)
{
int j,k,v;
if(s[i]=='?') v=(1<<26)-1;
else v=1<<(s[i]-'a');
for(j=0;j<26;j++)
{
sum[j]=0;
Arrays.fill(dp[cur][j],0);
}
for(j=0;j<26;j++)
for(k=0;k<26;k++)
sum[j]=(sum[j]+dp[cur^1][j][k])%M;
for(j=0;j<26;j++)
if(((v>>j)&1)==1)
{
for(k=0;k<26;k++)
if(k!=j) dp[cur][j][k]=(dp[cur][j][k]+(sum[k]-dp[cur^1][k][j]+M)%M)%M;
}
cur^=1;
}
long ans=0;
for(i=0;i<26;i++)
for(int j=0;j<26;j++)
ans=(ans+dp[cur^1][i][j])%M;
System.out.println(ans);
}
}
merlin