This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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()) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |