Submission #548719

# Submission time Handle Problem Language Result Execution time Memory
548719 2022-04-14T10:42:20 Z nishkarsh Type Printer (IOI08_printer) C++14
100 / 100
165 ms 51020 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define vi vector <int>
#define vl vector <ll>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d",x)
#define plld(x) printf("%lld",x)
#define pds(x) printf("%d ",x)
#define pllds(x) printf("%lld ",x)
#define pdn(x) printf("%d\n",x)
#define plldn(x) printf("%lld\n",x)
using namespace std;
ll powmod(ll base,ll exponent,ll mod){
	ll ans=1;
	if(base<0) base+=mod;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
ll gcd(ll a, ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
const int INF = 2e9;
const ll INFLL = 4e18;
const int upperlimit = 5e5+10;
const int mod = 1e9+7;
const int letters = 26;
int trie[upperlimit][letters];
bool to_print[upperlimit];
int maxlenchar[upperlimit];
int maxlen[upperlimit];
int node_cnt=0;
void addstring(string s){
	int node = 0,n=s.size();
	for(int i = 0; i < n; i++){
		if(maxlen[node] < n-i){
			maxlen[node] = n-i;
			maxlenchar[node] = s[i]-'a';
		}
		if(! trie[node][s[i]-'a']) trie[node][s[i]-'a']=++node_cnt;
		node=trie[node][s[i]-'a'];
	}
	to_print[node]=true;
}
void dfs(int node,int cur,bool del){
	if(node){
		char c = 'a'+cur;
		cout << c << "\n";
	}
	if(to_print[node]) cout << "P\n";
	for(int i = 0; i < letters; i++) if((trie[node][i])&&(i != maxlenchar[node])) dfs(trie[node][i],i,true);
	if(maxlen[node]) dfs(trie[node][maxlenchar[node]],maxlenchar[node],del);
	if(del) cout << "-\n";
}
int main(){
	int n;
	string s;
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> s;
		addstring(s);
	}
	cout << (2*node_cnt)+n-maxlen[0] << "\n";
	dfs(0,0,false);
	return 0;
}
# 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 1 ms 308 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 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1080 KB Output is correct
2 Correct 3 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3204 KB Output is correct
2 Correct 38 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 7628 KB Output is correct
2 Correct 10 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 18764 KB Output is correct
2 Correct 117 ms 42828 KB Output is correct
3 Correct 95 ms 22272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 14800 KB Output is correct
2 Correct 165 ms 51020 KB Output is correct
3 Correct 71 ms 25264 KB Output is correct
4 Correct 119 ms 48084 KB Output is correct