답안 #1016602

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016602 2024-07-08T08:54:16 Z NintsiChkhaidze Type Printer (IOI08_printer) C++17
100 / 100
930 ms 58160 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;
bool fin[500005];
string s[N],str;
map <int,int> mp[500005];
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 (mp[root].find(b) == mp[root].end()){
			mp[root][b] = ++cnt;
		}

		root = mp[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 (mp[x].find(j) == mp[x].end()) continue;
		if (j != b || (j == b && !eq)){
			ans.pb(char(j + 'a' - 1));
			dfs(mp[x][j],i + 1,0);
		}
	}

	if (eq) {
		ans.pb(char(b + 'a' - 1));
		dfs(mp[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:24:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for (int i = 0; i < x.size(); i++){
      |                  ~~^~~~~~~~~~
printer.cpp: In function 'void dfs(int, int, bool)':
printer.cpp:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  if (eq == 1 && i == str.size()) {
      |                 ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33372 KB Output is correct
2 Correct 5 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33372 KB Output is correct
2 Correct 5 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33368 KB Output is correct
2 Correct 5 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33372 KB Output is correct
2 Correct 5 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33372 KB Output is correct
2 Correct 13 ms 33624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 33624 KB Output is correct
2 Correct 25 ms 33948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 34652 KB Output is correct
2 Correct 125 ms 36432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 36816 KB Output is correct
2 Correct 46 ms 34392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 42308 KB Output is correct
2 Correct 793 ms 54212 KB Output is correct
3 Correct 448 ms 44660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 293 ms 40144 KB Output is correct
2 Correct 926 ms 58160 KB Output is correct
3 Correct 523 ms 46012 KB Output is correct
4 Correct 930 ms 56852 KB Output is correct