Submission #310649

# Submission time Handle Problem Language Result Execution time Memory
310649 2020-10-07T14:29:02 Z Akashi Type Printer (IOI08_printer) C++14
100 / 100
367 ms 99888 KB
#include <bits/stdc++.h>
using namespace std;

struct Trie {
	Trie *fii[26];
	int cnt, mx;

	Trie(){memset(fii, NULL, sizeof(fii)); cnt = mx = 0;}
};

Trie *T = new Trie;

int t;
char s[25];

void add(Trie *nod, char *p){
	if (*p == NULL) {
		nod->cnt++;
		return ;
	}

	if (nod->fii[*p - 'a'] == NULL) nod->fii[*p - 'a'] = new Trie;
	add(nod->fii[*p - 'a'], p + 1);	
	nod->mx = max(nod->mx, nod->fii[*p - 'a']->mx + 1);
}

string sol;
void parc(Trie *nod = T) {
	if (nod->cnt)	sol.push_back('P');
	
	int wh = -1, mx = -1;
	int cnt = 0;
	for (int i = 0; i < 26 ; ++i) {
		if (nod->fii[i] == NULL) continue ;
		if (nod->fii[i]->mx > mx) mx = nod->fii[i]->mx, wh = i;
		++cnt;
	}

	if (cnt > 1) cerr << cnt << " " << mx << " " << (char)(wh + 'a') << endl;

	if (wh == -1) return ;

	for (int i = 0; i < 26 ; ++i) {
		if (nod->fii[i] == NULL || i == wh) continue ;
		sol.push_back(i + 'a');
		parc(nod->fii[i]);
		sol.push_back('-');
	}

	sol.push_back(wh + 'a');
	parc(nod->fii[wh]);
	sol.push_back('-');
}

int main() {
	scanf("%d", &t);
	for (int i = 1; i <= t ; ++i) {
		scanf("%s", s);
		add(T, s);	
	}
	
	parc();

	while (sol.back() == '-') sol.pop_back();

	printf("%d\n", sol.size());
	for (auto it : sol) printf("%c\n", it);

	return 0;
}

Compilation message

printer.cpp: In constructor 'Trie::Trie()':
printer.cpp:8:21: warning: passing NULL to non-pointer argument 2 of 'void* memset(void*, int, size_t)' [-Wconversion-null]
    8 |  Trie(){memset(fii, NULL, sizeof(fii)); cnt = mx = 0;}
      |                     ^~~~
In file included from /usr/include/features.h:367,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/c++config.h:524,
                 from /usr/include/c++/9/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from printer.cpp:1:
/usr/include/x86_64-linux-gnu/bits/string3.h:78:1: note:   declared here
   78 | __NTH (memset (void *__dest, int __ch, size_t __len))
      | ^~~~~
printer.cpp: In function 'void add(Trie*, char*)':
printer.cpp:17:12: warning: NULL used in arithmetic [-Wpointer-arith]
   17 |  if (*p == NULL) {
      |            ^~~~
printer.cpp: In function 'int main()':
printer.cpp:66:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wformat=]
   66 |  printf("%d\n", sol.size());
      |          ~^     ~~~~~~~~~~
      |           |             |
      |           int           std::__cxx11::basic_string<char>::size_type {aka long unsigned int}
      |          %ld
printer.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   56 |  scanf("%d", &t);
      |  ~~~~~^~~~~~~~~~
printer.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   58 |   scanf("%s", s);
      |   ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 3 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1792 KB Output is correct
2 Correct 8 ms 2304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6136 KB Output is correct
2 Correct 42 ms 12560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 14856 KB Output is correct
2 Correct 11 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 36756 KB Output is correct
2 Correct 303 ms 83888 KB Output is correct
3 Correct 102 ms 43284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 28816 KB Output is correct
2 Correct 367 ms 99888 KB Output is correct
3 Correct 115 ms 49172 KB Output is correct
4 Correct 289 ms 94176 KB Output is correct