답안 #698940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698940 2023-02-14T23:47:23 Z jcelin Type Printer (IOI08_printer) C++14
100 / 100
177 ms 120192 KB
#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define ii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define vpll vector<pll>
#define msi multiset<int>
#define si set<int>
#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int dxx[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dyy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 7;
const int inf = mod;
const ll INF = 1e18;
const int logo = 20;
const int off = 1 << logo;
const int trsz = off << 1;

struct node{
	node *ch[30];
	int id;
	bool ms;
	int dp, bs;
	node(){
		REP(i, 30) ch[i] = NULL;
		dp = 0;
		bs = -1;
		ms = 0;
		id = 0;
	}
};

string s;
node* rt;
int timer = 1;

void add(node *x, int d){
	if(x -> id == 0) x -> id = timer++;
	if(d > s.size()){
		x -> ms = 1;
		return;
	}
	int nx = s[d - 1] - 'a';
	if(x -> ch[nx] == NULL) x -> ch[nx] = new node();
	add(x -> ch[nx], d + 1);
}

void dfs(node *u){
	vi chi;
	REP(i, 30) if(u -> ch[i] != NULL) dfs(u -> ch[i]), chi.PB(i);	
	
	int s1 = 0;
	for(auto &x : chi){
		int ps = s1;
		s1 = max(s1, (u -> ch[x]) -> dp + 1);
		if(ps != s1) u -> bs = x;
	}
	u -> dp = s1;
}

string sol;
void constr(node *u, bool fl = 0){
	if(u -> ms) sol.PB('P');
	REP(i, 30) if(u -> ch[i] != NULL) if(i != u -> bs) sol.PB('a' + i), constr(u -> ch[i], 1);
	bool nfl = 1;
	if(fl == 0) nfl = 0;
	if(u -> bs != -1) sol.PB('a' + u -> bs), constr(u -> ch[u -> bs], nfl);
	if(fl) sol.PB('-');
}

void solve(){
	int n;
	cin >> n;
	rt = new node();
	REP(i, n) cin >> s, add(rt, 1);
	dfs(rt);
	constr(rt, 0);
	cout << sol.size() << "\n";
	for(auto &x : sol) cout << x << "\n";
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t=1;
	//cin >> t;
	while(t--)solve();
	return 0;
}






Compilation message

printer.cpp: In function 'void add(node*, int)':
printer.cpp:63:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |  if(d > s.size()){
      |     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2132 KB Output is correct
2 Correct 5 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 7124 KB Output is correct
2 Correct 25 ms 14992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 17680 KB Output is correct
2 Correct 11 ms 3916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 44172 KB Output is correct
2 Correct 152 ms 101032 KB Output is correct
3 Correct 83 ms 52012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 34516 KB Output is correct
2 Correct 177 ms 120192 KB Output is correct
3 Correct 93 ms 59100 KB Output is correct
4 Correct 164 ms 113416 KB Output is correct