QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#277215 | #4275. Escape Sequences | PetroTarnavskyi# | ML | 1ms | 3468kb | C++20 | 2.7kb | 2023-12-06 16:39:25 | 2023-12-06 16:39:25 |
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 AL = 2;
struct Node
{
int g[AL];
int link;
int len;
int cnt;
void init()
{
fill(g, g + AL, -1);
link = -1;
len = -1;
cnt = 1;
}
};
struct Automaton
{
vector<Node> a;
int sz;
int head;
void init(int n)
{
a.resize(2 * n);
a[0].init();
sz = 1;
head = 0;
}
void add(char c)
{
// change to [0 AL)
int ch = c - 'a';
int nhead = sz++;
a[nhead].init();
a[nhead].len = a[head].len + 1;
int cur = head;
head = nhead;
while (cur != -1 && a[cur].g[ch] == -1)
{
a[cur].g[ch] = head;
cur = a[cur].link;
}
if (cur == -1)
{
a[head].link = 0;
return;
}
int p = a[cur].g[ch];
if (a[p].len == a[cur].len + 1)
{
a[head].link = p;
return;
}
int q = sz++;
a[q] = a[p];
a[q].cnt = 0;
a[q].len = a[cur].len + 1;
a[p].link = a[head].link = q;
while (cur != -1 && a[cur].g[ch] == p)
{
a[cur].g[ch] = q;
cur = a[cur].link;
}
}
} a;
const int N = 200'500;
int ans = N;
bool check(const string& t)
{
int v = 0;
FOR (i, 0, SZ(t))
{
if (a.a[v].g[t[i] - 'a'] != -1)
v = a.a[v].g[t[i] - 'a'];
else
return false;
}
return true;
}
void f(string t, int d)
{
if (check(t))
{
ans = min(ans, d);
return;
}
if (SZ(t) == 1)
{
if (t[0] == 'a')
ans = min(ans, d + 1);
return;
}
bool odd = false;
bool even = false;
for (int i = 0; i < SZ(t); i++)
{
if (t[i] == 'b')
{
if (i & 1)
odd = 1;
else
even = 1;
}
}
if (!odd && !even)
{
string s(SZ(t) / 2, 'a');
if (SZ(t) & 1)
{
s += 'a';
f(s, d + 1);
s.pop_back();
s += 'b';
f(s, d + 1);
}
else
{
f(s, d + 1);
}
return;
}
if (odd && even)
return;
string s = "";
FOR (i, 0, SZ(t))
{
if ((i & 1) && (odd))
{
s += t[i];
}
if (i % 2 == 0 && even)
{
s += t[i];
}
}
if ((SZ(t) % 2 == 1 && odd) || (SZ(t) % 2 == 0 && even))
{
s += 'a';
f(s, d + 1);
s.pop_back();
s += 'b';
f(s, d + 1);
}
else
{
f(s, d + 1);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string s, t;
cin >> s >> t;
a.init(SZ(s));
FOR (i, 0, SZ(s))
a.add(s[i]);
f(t, 0);
if (ans == N)
cout << -1 << '\n';
else
cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3428kb
input:
b ab
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
ababa bab
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3424kb
input:
a b
output:
-1
result:
ok 1 number(s): "-1"
Test #4:
score: -100
Memory Limit Exceeded
input:
abbb baa