QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#607958#2428. Comma SprinklerLJY_ljyWA 50ms80256kbC++112.6kb2024-10-03 17:19:262024-10-03 17:19:27

Judging History

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

  • [2024-10-03 17:19:27]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:80256kb
  • [2024-10-03 17:19:26]
  • 提交

answer

//Trie树(字典树)练习 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
using namespace std;

const int MAXN = 1000010;
char str[MAXN], Ans[MAXN];
int exist[MAXN], End[MAXN], len, Bound[MAXN];
int eid, cnt, nex[MAXN][30], Type[MAXN], type[MAXN], Eid;
vector<int> G[MAXN][2];
bool E[MAXN][2];
queue<int> q1, q2;
// 空格 0, 句号 1,逗号 2

int main() {
	for (int i = 1; i < MAXN; i++) exist[i] = -1; 
	while (scanf("%s", str + 1) != EOF) {
		int n = strlen(str + 1), m;
		cnt++;
		if ('a' <= str[n] && str[n] <= 'z') m = n;
		else m = n - 1;
		Type[cnt] = (Type[cnt - 1] % 3) * 3;
		if ('.' == str[n]) Type[cnt] ++;
		if (',' == str[n]) Type[cnt] += 2; 
		for (int i = len + 1; i <= len + m; i++)
			Ans[i] = str[i - len];
		len = len + m;
		Bound[cnt] = len;
		int p = 0;
		for (int i = 1; i <= m; i++) {
			int c = str[i] - 'a';
			if (!nex[p][c]) nex[p][c] = ++eid;
			p = nex[p][c];
 		}
 		if (exist[p] == -1)
 			type[p] = ++Eid;
 		if (exist[p] == -1)
		 	exist[p] = 0;
 		if (Type[cnt] % 3 == 2 && exist[p] % 3 == 0)
 			exist[p] += 2;
 		if (Type[cnt] / 3 == 2 && exist[p] / 3 == 0)
 			exist[p] += 6; 
 		End[cnt] = p;
	}
	for (int i = 1; i <= cnt; i++) {
		if (Type[i + 1] / 3 != 1 && !E[type[End[i + 1]]][0]) {
			G[type[End[i]]][0].push_back(type[End[i + 1]]); // ..., -> ,... 
			E[type[End[i + 1]]][0] = true;
		}
		if (Type[i - 1] % 3 != 1 && !E[type[End[i - 1]]][1]) {
			G[type[End[i]]][1].push_back(type[End[i - 1]]); // ,... -> ...,
			E[type[End[i - 1]]][1] = true;
		}
	} 
	for (int i = 1; i <= Eid; i++) E[i][0] = E[i][1] = false;
	for (int i = 1; i <= Eid; i++) {
		if (exist[End[i]] % 3 == 2 && !E[type[End[i]]][0]) {
			q1.push(type[End[i]]);
			E[type[End[i]]][0] = true;
		}
		if (exist[End[i]] / 3 == 2 && !E[type[End[i]]][1]) {
			q2.push(type[End[i]]);
			E[type[End[i]]][1] = true;
		} 
	}
	while ((!q1.empty()) || (!q2.empty())) {
		while (!q1.empty()) {
			int x = q1.front();
			for (auto y:G[x][0]) {
				if (!E[y][1]) {
					E[y][1] = true;
					q2.push(y);
				}
			}
			q1.pop();
		}
		while (!q2.empty()) {
			int x = q2.front();
			for (auto y:G[x][1]) {
				if (!E[y][0]) {
					E[y][0] = true;
					q1.push(y);
				}
			}
			q2.pop();
		}
	} 
	for (int i = 1; i <= cnt; i++) {
		for (int j = Bound[i - 1] + 1; j <= Bound[i]; j++)
			printf("%c", Ans[j]); 
		if (Type[i] % 3 != 1 && (E[type[End[i]]][0] || E[type[End[i + 1]]][1])) printf(",");
		if (Type[i] % 3 == 1) printf(".");
		printf(" ");
	}
	return 0;
}  

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 59072kb

Test #2:

score: 0
Accepted
time: 4ms
memory: 59060kb

Test #3:

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

Test #4:

score: 0
Accepted
time: 4ms
memory: 57084kb

Test #5:

score: 0
Accepted
time: 11ms
memory: 59056kb

Test #6:

score: 0
Accepted
time: 47ms
memory: 66624kb

Test #7:

score: 0
Accepted
time: 48ms
memory: 80256kb

Test #8:

score: -100
Wrong Answer
time: 50ms
memory: 78996kb