Submission #476449

# Submission time Handle Problem Language Result Execution time Memory
476449 2021-09-27T04:09:28 Z chenwz Type Printer (IOI08_printer) C++11
100 / 100
108 ms 49356 KB
#include <bits/stdc++.h>
using namespace std;
const int SIG = 26, NN = 25000;
struct Node {
  int ch[SIG];
  bool mark, end;
} T[NN * 20];
int sz = 0;
void insert(const string &s) {
  int u = 0;
  for (char c : s) {
    int d = c - 'a', &cu = T[u].ch[d];
    if (!cu) cu = ++sz;
    u = cu;
  }
  T[u].end = true;
}
void dfs(int u, string& ans) {
  const Node& x = T[u];
  if (x.end) ans += 'P'; // 单词结尾, 打印
  int mi = -1;
  for (int i = 0; i < SIG; ++i) {
    int v = x.ch[i];
    if(!v) continue;
    if (T[v].mark) mi = i; // 先不走最长的串
    else ans += i + 'a', dfs(v, ans); // 走其它串
  }
  if (mi != -1) // 走最长串
    ans += mi + 'a', dfs(x.ch[mi], ans);
  ans += '-';
}

int main() {  
  char B[24];
  int n; scanf("%d", &n);
  string s, l;  
  for (int i = 0; i < n; ++i) {
    scanf("%s", B), s = string(B), insert(s);
    if (s.length() > l.length()) l = s;
  }
  for(size_t i = 0, u = 0; i < l.size(); i++) // 标记最长的单词
    u = T[u].ch[l[i] - 'a'], T[u].mark = true;
  s.clear(), dfs(0, s);
  while(s.back() == '-') s.pop_back(); // 最后的字符全部删掉
  printf("%lu\n", s.size());
  for (char c : s) printf("%c\n", c);
  return 0;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:35:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |   int n; scanf("%d", &n);
      |          ~~~~~^~~~~~~~~~
printer.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     scanf("%s", B), s = string(B), insert(s);
      |     ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 972 KB Output is correct
2 Correct 3 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3020 KB Output is correct
2 Correct 14 ms 6328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 7496 KB Output is correct
2 Correct 7 ms 1740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 18220 KB Output is correct
2 Correct 96 ms 41452 KB Output is correct
3 Correct 52 ms 21340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 14336 KB Output is correct
2 Correct 108 ms 49356 KB Output is correct
3 Correct 63 ms 24784 KB Output is correct
4 Correct 96 ms 47016 KB Output is correct