QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#450967 | #6821. Another A+B Problem | PetroTarnavskyi# | WA | 0ms | 3612kb | C++20 | 2.6kb | 2024-06-22 20:01:23 | 2024-06-22 20:01:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int mod = 998244353;
int mult(int a, int b)
{
return (LL)a * b % mod;
}
void updAdd(int& a, int b)
{
a += b;
if(a >= mod)
a -= mod;
}
const int AL = 26;
struct Node
{
int p;
int c;
int g[AL];
int nxt[AL];
int link;
Node(int _c, int _p)
{
c = _c;
p = _p;
fill(g, g + AL, -1);
fill(nxt, nxt + AL, -1);
link = -1;
}
};
struct AC
{
vector<Node> a;
VI term;
VI len;
void init(int n)
{
a.reserve(n);
a.PB(Node(-1, -1));
term.PB(0);
len.PB(0);
}
int addStr(const string& s, int id)
{
int v = 0;
FOR(i, 0, SZ(s))
{
int c = s[i] - 'a';
if(a[v].nxt[c] == -1)
{
a[v].nxt[c] = SZ(a);
a.PB(Node(c, v));
term.PB(0);
len.PB(0);
}
v = a[v].nxt[c];
}
term[v] = id;
len[v] = SZ(s);
return v;
}
int go(int v, int c)
{
if(a[v].g[c] != -1)
return a[v].g[c];
if(a[v].nxt[c] != -1)
a[v].g[c] = a[v].nxt[c];
else if(v != 0)
a[v].g[c] = go(getLink(v), c);
else
a[v].g[c] = 0;
return a[v].g[c];
}
int getLink(int v)
{
if(a[v].link != -1)
return a[v].link;
if(v == 0 || a[v].p == 0)
return 0;
return a[v].link = go(getLink(a[v].p), a[v].c);
}
vector<VI> g;
VI par;
void dfs(int v, int p)
{
par[v] = p;
if(term[v] != 0)
p = v;
for(int to : g[v])
dfs(to, p);
}
void build()
{
g.resize(SZ(a));
par.resize(SZ(a));
FOR(i, 1, SZ(a))
g[getLink(i)].PB(i);
dfs(0, -1);
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
string s;
cin >> s >> n;
AC A;
A.init(n);
VI value(n + 1);
FOR(i, 0, n)
{
string t;
cin >> t >> value[i + 1];
A.addStr(t, i + 1);
}
A.build();
VI dp(SZ(s) + 1);
dp[0] = 1;
int v = 0;
FOR(i, 0, SZ(s))
{
v = A.go(v, s[i] - 'a');
dp[i + 1] = dp[i];
if(A.term[v])
updAdd(dp[i + 1], mult(value[A.term[v]], dp[i + 1 - A.len[v]]));
int x = v;
while(true)
{
x = A.par[x];
if(x == -1)
break;
updAdd(dp[i + 1], mult(value[A.term[x]], dp[i + 1 - A.len[x]]));
}
if(i)
cout << " ";
cout << dp[i + 1];
}
cout << "\n";
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3612kb
input:
40+11=51 PBGPPGGB
output:
1 1 1 1 1 1 1 1
result:
wrong answer Jury has 7 answers but participant has 1