結果
| 問題 |
No.1677 mæx
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-09-10 22:38:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 17 ms / 2,000 ms |
| コード長 | 1,462 bytes |
| コンパイル時間 | 818 ms |
| コンパイル使用メモリ | 72,704 KB |
| 実行使用メモリ | 11,776 KB |
| 最終ジャッジ日時 | 2024-06-12 01:44:59 |
| 合計ジャッジ時間 | 1,536 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
int mex(int a, int b){
if(a > b) swap(a, b);
if(a > 0) return 0;
if(b == 1) return 2;
return 1;
}
vector<int> d[100000];
vector<ll> rec(int l, int r, int k, string &s){
vector<ll> res(3);
if(r - l == 1){
if(s[l] == '?'){
for(int a = 0; a < 3; a++) res[a]++;
}
else res[s[l] - '0']++;
}
else{
int m = *lower_bound(d[k].begin(), d[k].end(), l);
vector<ll> p = rec(l + 4, m, k + 1, s);
vector<ll> q = rec(m + 1, r - 1, k + 1, s);
if(s[l + 1] != 'a'){
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
int c = mex(a, b);
res[c] = (res[c] + p[a] * q[b]) % MOD;
}
}
}
if(s[l + 1] != 'e'){
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
int c = max(a, b);
res[c] = (res[c] + p[a] * q[b]) % MOD;
}
}
}
}
return res;
}
int main()
{
string s;
int k;
cin >> s >> k;
int n = s.size();
int h = 0;
for(int i = 0; i < n; i++){
if(s[i] == '(') h++;
if(s[i] == ')') h--;
if(s[i] == ',') d[h].push_back(i);
}
vector<ll> ans = rec(0, n, 1, s);
cout << ans[k] << endl;
}