Submission #982721

# Submission time Handle Problem Language Result Execution time Memory
982721 2024-05-14T16:38:35 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
73 ms 49924 KB
#include<iostream>
#include<string>
#include<algorithm>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int n;
struct node{
	bool w, l;
	int hijos[26];
};
node trie[500002];
int t;

int maxi;
string word, larger;

void mete( string word, bool p ){
	
	int pos = 0;
	for( int i = 0; i < word.size(); i++ ){
		int letra = ( word[i] - 'a' );
		trie[pos].hijos[letra] = trie[pos].hijos[letra] ? trie[pos].hijos[letra] : ++t;
		pos = trie[pos].hijos[letra];
		trie[pos].l = p;
	}
	
	trie[pos].w = true;
	trie[pos].l = p;

}

string ans;

void recorre( int nodo ){
	
	if( trie[nodo].w )
		ans+= "P";
	int pending = -1, hijo;
	
	for( int i = 0; i < 26; i++ ){
		
		if( trie[nodo].hijos[i] ){
			hijo = trie[nodo].hijos[i];
			if( trie[hijo].l ){
				pending = i;
				continue;
			}
			ans.push_back( (char)(i) + 'a' );
			recorre( hijo );
			ans += "-";
		}
		
	}
	
	if( pending != -1 ){
		ans.push_back( (char)(pending) + 'a' );
		recorre( trie[nodo].hijos[pending] );
	}
	
}

int main(){
	
	optimizar_io
	cin >> n;
	for( int i = 1; i <= n; i++ ){
		cin >> word;
		mete( word, 0 );
		if( word.size() > maxi ){
			maxi = word.size();
			larger = word;
		}
	}
	mete( larger, 1 );
	
	recorre( 0 );
	cout << ans.size() << "\n";
	for( int i = 0; i < ans.size(); i++ )
		cout << ans[i] << "\n";
	
	return 0;
	
}

Compilation message

printer.cpp: In function 'void mete(std::string, bool)':
printer.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for( int i = 0; i < word.size(); i++ ){
      |                  ~~^~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:70:19: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   70 |   if( word.size() > maxi ){
      |       ~~~~~~~~~~~~^~~~~~
printer.cpp:79:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for( int i = 0; i < ans.size(); i++ )
      |                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Correct 8 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9196 KB Output is correct
2 Correct 5 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 18424 KB Output is correct
2 Correct 61 ms 42004 KB Output is correct
3 Correct 32 ms 22772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 14384 KB Output is correct
2 Correct 73 ms 49924 KB Output is correct
3 Correct 44 ms 25068 KB Output is correct
4 Correct 64 ms 47284 KB Output is correct