Submission #555974

# Submission time Handle Problem Language Result Execution time Memory
555974 2022-05-02T04:30:31 Z iDoItSaNdWiTcH Type Printer (IOI08_printer) C++17
90 / 100
1000 ms 99504 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = 1e9 + 5;

//////////////////////////////////////////////////////////////////////////////////////////
string cur = "";

struct Trie {
	int totalsz = 0;
	bool isend = 0;
	struct Trie* children[26] = {};
};

void tinsert(struct Trie* root, string s) {
	//find the bug in tinsert
	
	Trie* cnt = root;
	for (int i = 0; i < s.length(); i++) {
		if (cnt->children[s[i] - 'a'] == nullptr) {
			//create new tree
			cnt->children[s[i] - 'a'] = new Trie();
		}

		//calculate how much is left]
		int sz = cnt->children[s[i] - 'a']->totalsz;
		
		if (s.length() - i - 1 > sz) {
			(cnt->children[s[i] - 'a']->totalsz) = s.length() - i - 1;
		}
		cnt = cnt->children[s[i] - 'a'];
	}
	cnt->isend = 1;
}

void dfs(Trie* root) {
	//theres a string that ends here
	if (root->isend) {
		cur += "P";
	}
	
	//first find the one with longest string
	int longestsz = 0;
	int c = 0;
	for (int i = 0; i < 26; i++) {
		if (root->children[i] != nullptr) {
			if (root->children[i]->totalsz > longestsz) {
				longestsz = root->children[i]->totalsz;
				c = i;
			}
		}
	}

	//do the others
	for (int i = 0;i < 26; i++) {
		if (root->children[i] != nullptr && i != c) {
			cur += char('a' + i);
			dfs(root->children[i]);
			cur += "-";
		}
	}

	//last do back the longest string
	if (root->children[c] != nullptr) {
		cur += char('a' + c);
		dfs(root->children[c]);
		cur += "-";
	}
}

void solve(){	
	int n; cin >> n;

	struct Trie* root = new Trie();
	while (n--) {
		string s; cin >> s;

		tinsert(root, s);
	}

	dfs(root);
	int p = 0;
	
	for (int i = cur.length() - 1; i > -1; i--) {
		if (cur[i] == '-') p++;
		else break;
	}

	int maxx = cur.length() - p;
	cout << maxx << endl;

	for (int i = 0; i < maxx; i++) {
		cout << cur[i] << endl;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);


    int t = 1; 
    //cin >> t;
    while (t--){
    	solve();
   	}
} 

/*
find a method to dfs the longest strings at last
*/

Compilation message

printer.cpp: In function 'void tinsert(Trie*, std::string)':
printer.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |  for (int i = 0; i < s.length(); i++) {
      |                  ~~^~~~~~~~~~~~
printer.cpp:29:26: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |   if (s.length() - i - 1 > sz) {
      |       ~~~~~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 11 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 1840 KB Output is correct
2 Correct 23 ms 2268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 5964 KB Output is correct
2 Correct 152 ms 12516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 14788 KB Output is correct
2 Correct 47 ms 3384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 438 ms 36680 KB Output is correct
2 Correct 946 ms 83708 KB Output is correct
3 Correct 527 ms 43228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 28688 KB Output is correct
2 Execution timed out 1080 ms 99504 KB Time limit exceeded
3 Halted 0 ms 0 KB -