답안 #1016601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016601 2024-07-08T08:48:52 Z NintsiChkhaidze Type Printer (IOI08_printer) C++17
100 / 100
955 ms 67276 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
// #define int ll
using namespace std;

const int N = 3e5 + 5;

int lenmx,cnt = 1;
int t[500005][30],fin[500005];
string s[N],str;
vector <char> ans;

void add(string x){
	int root = 1;

	for (int i = 0; i < x.size(); i++){
		int b = (x[i] - 'a' + 1);
		if (!t[root][b]){
			t[root][b] = ++cnt;
		}

		root = t[root][b];
	}
	fin[root] = 1;
}

void dfs(int x,int i,bool eq){

	if (fin[x]) ans.pb('P');
	if (eq == 1 && i == str.size()) {
		cout<<ans.size()<<endl;
		for (auto c: ans)
			cout<<c<<endl;
		exit(0);
	}

	int b = (str[i] - 'a' + 1);
	for (int j = 1; j <= 26; j++){
		if (!t[x][j]) continue;
		if (j != b || (j == b && !eq)){
			ans.pb(char(j + 'a' - 1));
			dfs(t[x][j],i + 1,0);
		}
	}

	if (eq) {
		ans.pb(char(b + 'a' - 1));
		dfs(t[x][b],i + 1,1);
	}

	ans.pb('-');
}

signed main() {
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

	int n;
	cin>>n;

	for (int i = 1; i <= n; i++){
		cin >> s[i];
		add(s[i]);
		int len = s[i].size();
		if (len > lenmx){
			str = s[i];
			lenmx = len;
		}
	}

	dfs(1,0,1);
}

Compilation message

printer.cpp: In function 'void add(std::string)':
printer.cpp:23:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for (int i = 0; i < x.size(); i++){
      |                  ~~^~~~~~~~~~
printer.cpp: In function 'void dfs(int, int, bool)':
printer.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |  if (eq == 1 && i == str.size()) {
      |                 ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 10 ms 11152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11612 KB Output is correct
2 Correct 20 ms 11864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 13916 KB Output is correct
2 Correct 115 ms 17672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 18652 KB Output is correct
2 Correct 40 ms 12832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 344 ms 30900 KB Output is correct
2 Correct 790 ms 58296 KB Output is correct
3 Correct 403 ms 35532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 26332 KB Output is correct
2 Correct 955 ms 67276 KB Output is correct
3 Correct 451 ms 38868 KB Output is correct
4 Correct 850 ms 64120 KB Output is correct