# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
941939 | MuntherCarrot | Palindromes (APIO14_palindrome) | C++14 | 44 ms | 69036 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define INFLL (ll)0x3f3f3f3f3f3f3f3f
const int MOD = 1e9 + 7, SZ = 1e5 + 10;
// Trie v0.1
struct trie{
vector<vector<int>> Trie;
vector<int> End;
vector<char> ans;
int MAXN, node_cnt;
string big;
trie(int N) : MAXN(N), node_cnt(0) {
Trie.resize(MAXN, vector<int>(26));
End.resize(MAXN);
}
void insert(string &word){
int node = 0;
for(int i = 0; i < word.size(); i++){
if(Trie[node][word[i] - 'a'] == 0){
Trie[node][word[i] - 'a'] = ++node_cnt;
}
node = Trie[node][word[i] - 'a'];
}
End[node]++;
}
void solve(int node = 0, int x = 0){
for(int i = 0; i < End[node]; i++){
ans.push_back('P');
}
for(int i = 0; i < 26; i++){
if(x != -1 && (char)('a' + i) == big[x]) continue;
if(Trie[node][i] == 0) continue;
ans.push_back((char)('a' + i));
solve(Trie[node][i], -1);
ans.push_back('-');
}
if(x != -1 && x != big.size()){
ans.push_back(big[x]);
solve(Trie[node][big[x] - 'a'], x + 1);
}
}
} Trie(25000 * 20);
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, mx = 0;
cin >> n;
string s[n];
for(auto &i : s){
cin >> i;
Trie.insert(i);
if(mx < i.size()){
mx = i.size();
Trie.big = i;
}
}
Trie.solve();
cout << Trie.ans.size() << endl;
// cout << Trie.ans.size() - mx << endl;
for(int i = 0; i < Trie.ans.size(); i++){
cout << Trie.ans[i] << endl;
}
return 0;
}
// by me
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |