QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#556797#6816. Invincible HotwheelsZepX_DWA 7ms46836kbC++143.0kb2024-09-10 21:04:012024-09-10 21:04:02

Judging History

你现在查看的是最新测评结果

  • [2024-09-10 21:04:02]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:46836kb
  • [2024-09-10 21:04:01]
  • 提交

answer

#include <bits/stdc++.h>
#define fr first
#define se second
#define U unsigned
#define LL long long
#define pb push_back
#define pii pair<int,int>
#define __ __int128

using namespace std;

inline int read()
{
	int x = 0;char ch = getchar();
	while(!isdigit(ch)) ch = getchar();
	while(isdigit(ch)) x = x*10+ch-48,ch = getchar();
	return x;
}

const int N = 1e6+5;
int ed[N],edp[N/10],tr[N][26],cnt,fa[N],dfn[N],d,siz[N],len[N/10];
pii pre[N];
string s[N/10];
vector < int > ve[N];

void ins(int id)
{
	int p = 0;
	for (char c : s[id])
	{
		int x = c-'a';
		if(!tr[p][x]) tr[p][x] = ++cnt;
		p = tr[p][x];
	}
	ed[p] = id,edp[id] = p,pre[p].fr = id;
}

void build()
{
	queue < int > q;
	for (int i = 0;i < 26;i++)
		if(tr[0][i]) q.push(tr[0][i]);
	while(q.size())
	{
		int x = q.front();q.pop();
		ve[fa[x]].pb(x);
		if(!ed[x]) pre[x] = pre[fa[x]];
		else pre[x].se = pre[fa[x]].fr;
		for (int i = 0;i < 26;i++)
		{
			if(tr[x][i]) fa[tr[x][i]] = tr[fa[x]][i],q.push(tr[x][i]);
			else tr[x][i] = tr[fa[x]][i];
		}
	}
}

void D(int x)
{
	dfn[x] = ++d,siz[x] = 1;
	for (int y : ve[x]) D(y),siz[x] += siz[y];
}

int c1[N],c2[N];

void A1(int i,int k){while(i <= d) c1[i] += k,i += i&-i;}

int Q1(int i)
{
	int res = 0;
	while(i) res += c1[i],i -= i&-i;
	return res;
}

int W(int a,int b)
{
	if(a == -1 || b == -1) return -1;
	if(!a || !b) return a|b;
	if(a == b) return a;
	return -1;
}

int Len;
void A2(int i,int k){while(i) c2[i] = W(c2[i],k),i -= i&-i;}

int Q2(int i)
{
	int res = 0;
	while(i <= Len) res = W(res,c2[i]),i += i&-i;
	return res;
}

struct seg
{
	int l,r,id;
	bool operator < (const seg &b) const
	{return l == b.l ? r > b.r : l < b.l;}
}a[N<<1];

int b[N],bl[N];
bool vis[N];

int main()
{
	int n = read(),ans = 0;
	for (int i = 1;i <= n;i++)
		cin >> s[i],ins(i),len[i] = s[i].size();
	build(),D(0);
	for (int i = 1;i <= n;i++)
	{
		int p = 0,ct1 = 0,ct2 = 0;
		for (int j = 0;j < len[i];j++)
		{
			int x = s[i][j]-'a';p = tr[p][x];
			if(j == len[i]-1) p = fa[p];
			auto [p1,p2] = pre[p];
			if(p2)
			{
				A1(dfn[fa[edp[p2]]],1);
				if(p2 != i)
				{
					if(!vis[p2]) b[++ct2] = p2,vis[p2] = 1,bl[p2] = 0;
					a[++ct1] = {j-len[p2]+2,j+1,p2};
				}
			}
			if(p1)
			{
				if(p1 != i)
				{
					if(!vis[p1]) b[++ct2] = p1,vis[p1] = 1,bl[p1] = 0;
					a[++ct1] = {j-len[p1]+2,j+1,p1};
				}
			}
		}
		sort(a+1,a+ct1+1);
		Len = len[i];
		for (int j = 1;j <= Len;j++) c2[j] = 0;
		for (int j = 1;j <= ct1;j++)
		{
			bl[a[j].id] = W(bl[a[j].id],Q2(a[j].r));
			A2(a[j].r,a[j].id);
		}
		for (int j = 1;j <= ct2;j++)
		{
			vis[b[j]] = 0;int p = edp[b[j]];
			if(Q1(dfn[p]+siz[p]-1)-Q1(dfn[p]) || bl[b[j]] <= 0) continue;
			ans++;
		}
		p = 0;
		for (int j = 0;j < len[i];j++)
		{
			int x = s[i][j]-'a';p = tr[p][x];
			if(j == len[i]-1) p = fa[p];
			auto [p1,p2] = pre[p];
			if(p2) A1(dfn[fa[edp[p2]]],-1);
		}
	}
	printf("%d",ans);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 46836kb

input:

8
wwwsoupunetcom
wwwsoupunet
soupunet
punetcom
punet
pun
net
n

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 3ms
memory: 42692kb

input:

4
a
aa
aaa
aaaa

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 7ms
memory: 45008kb

input:

5
bc
cbcb
cbca
cbc
c

output:

5

result:

wrong answer 1st numbers differ - expected: '3', found: '5'