QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#277213#4275. Escape SequencesPetroTarnavskyi#Compile Error//Python32.7kb2023-12-06 16:38:592023-12-06 16:38:59

Judging History

This is the latest submission verdict.

  • [2023-12-06 16:38:59]
  • Judged
  • [2023-12-06 16:38:59]
  • Submitted

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;
}

详细

  File "answer.code", line 49
    		// change to [0 AL)
    		                  ^
SyntaxError: closing parenthesis ')' does not match opening parenthesis '['