답안 #958386

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958386 2024-04-05T17:07:20 Z mkupanovac Type Printer (IOI08_printer) C++14
100 / 100
158 ms 108096 KB
#include<bits/stdc++.h>
#define ll long long
#define PB push_back
#define X first
#define Y second
#define MP make_pair
#define pii pair<int, int>
 
const int MAXN = (1e5 + 10);
const int INF = (1e9 + 7);
 
using namespace std;
 
 
int n;
vector<string> v;
vector<char> sol;
 
struct cvor{
	char slovo;
	int br;
	int slispod = 0;
	cvor* djeca[26];
	cvor(char slovo2){
		slovo = slovo2;
		br = 0;
		slispod = 0;
		for (int i = 0; i < 26; i++){
			djeca[i] = nullptr;
		}
	}
};
 
cvor root('\0');
 
void dodaj(string s){
	cvor* tr = &root;
	for (auto x: s){
		
		if (tr -> djeca[x - 'a'] == nullptr){
			tr -> djeca[x - 'a'] = new cvor(x);
		}
		tr = tr -> djeca[x - 'a'];
		tr -> br++;
	}
}
 
int ispod(cvor* tr){
	if (tr -> slispod)
		return tr -> slispod;
	
	int trisp = 0;
	for (int i = 0; i < 26; i++){
		if (tr -> djeca[i] != nullptr)
			trisp++;
	}
	//cout << tr -> slovo << " " << trisp << "\n";
	if (!trisp){
		//cout << tr -> slovo << " " << 0 << "\n";
		tr -> slispod = 0;
		return 0;
	}
	int trzbr = 0;
	
	for (int i = 0; i < 26; i++){
		if (tr -> djeca[i] != nullptr){
			int novi = 1 + ispod(tr -> djeca[i]);
			trzbr = max(trzbr, novi);
			//cout << tr -> slovo << " u " << tr -> djeca[i] -> slovo << " " << novi << " novi\n"; 
		}
			
	}
	//cout << tr -> slovo << " "	 << trzbr << "\n";
	tr -> slispod = trzbr;
	return trzbr;
}
 
 
void solve(cvor* tr){
	int uk = 0;
	
	vector<pii> trv;
	for (int i = 0; i < 26; i++){
		if (tr -> djeca[i] != nullptr){
			trv.PB(MP(tr -> djeca[i] -> slispod, i));
			uk += tr -> djeca[i] -> br;
		}
	}
	if (tr -> br - uk == 1){
		sol.PB('P');
	}
	sort (trv.begin(), trv.end());
	for (int i = 0; i < trv.size(); i++){
		//cout << trv[i].X << " za " << char('a' + trv[i].Y) << "   ";
		//cout << trv.size() << " " << trv[i].Y << "  i\n";
		sol.PB('a' + trv[i].Y);
		solve(tr -> djeca[trv[i].Y]);
		sol.PB('-');
	}
	//cout << "\n";
	/*if (!trv.size()){
		sol.PB('P');
	}*/
}
 
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n;
	for (int i = 0; i < n; i++){
		string s;
		cin >> s;
		v.PB(s);
		dodaj(s);
	}
	ispod(&root);
	//cout << ispod(&root) << " za root\n";
	
	solve(&root);
	int koliko = 0;
	for (int i = sol.size() - 1; i >= 0; i--){
		if (sol[i] == '-')
			koliko++;
		else
			break;
	}
	cout << sol.size() - koliko << "\n";
	for (int i = 0; i < sol.size() - koliko; i++){
		cout << sol[i] << "\n";
	}
	
	return 0;
}
 

Compilation message

printer.cpp: In function 'void solve(cvor*)':
printer.cpp:93:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |  for (int i = 0; i < trv.size(); i++){
      |                  ~~^~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:130:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |  for (int i = 0; i < sol.size() - koliko; i++){
      |                  ~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1880 KB Output is correct
2 Correct 3 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6444 KB Output is correct
2 Correct 17 ms 13784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 16232 KB Output is correct
2 Correct 7 ms 4056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 40384 KB Output is correct
2 Correct 123 ms 90944 KB Output is correct
3 Correct 66 ms 48064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 31944 KB Output is correct
2 Correct 144 ms 108096 KB Output is correct
3 Correct 78 ms 54336 KB Output is correct
4 Correct 158 ms 102144 KB Output is correct