QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#607573 | #2428. Comma Sprinkler | LJY_ljy | TL | 57ms | 21008kb | C++11 | 1.7kb | 2024-10-03 15:23:46 | 2024-10-03 15:23:48 |
Judging History
answer
//Trie树(字典树)练习
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
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];
// 空格 0, 句号 1,逗号 2
int main() {
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 (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;
}
while (true) {
bool flag = false;
for (int i = 1; i <= cnt; i++) {
if (exist[End[i + 1]] / 3 == 2 && exist[End[i]] % 3 == 0 && Type[i] % 3 != 1) {
exist[End[i]] += 2;
flag = true;
}
if (exist[End[i - 1]] % 3 == 2 && exist[End[i]] / 3 == 0 && Type[i] / 3 != 1) {
exist[End[i]] += 6;
flag = true;
}
}
if (!flag) break;
}
for (int i = 1; i <= cnt; i++) {
for (int j = Bound[i - 1] + 1; j <= Bound[i]; j++)
printf("%c", Ans[j]);
if ((exist[End[i]] % 3 == 2 && Type[i] % 3 != 1) || (exist[End[i + 1]] / 3 == 2 && Type[i + 1] / 3 != 1)) {
printf(",");
}
if (Type[i] % 3 == 1) printf(".");
printf(" ");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5824kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 5844kb
Test #3:
score: 0
Accepted
time: 1ms
memory: 5964kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 5908kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 5856kb
Test #6:
score: 0
Accepted
time: 57ms
memory: 14328kb
Test #7:
score: 0
Accepted
time: 39ms
memory: 21008kb
Test #8:
score: -100
Time Limit Exceeded