#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 200007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
ll cost(string s, string t)
{
string pref = "";
for(int i = 0; i < min(s.size(), t.size()); ++i){
if(s[i] != t[i]) break;
pref += s[i];
}
// cout << s << " " << t << " " << s.size() - pref.size() + t.size() - pref.size() << endl;
return s.size() - pref.size() + t.size() - pref.size();
}
vector <char> ans;
string s[M];
ll n, dp[M][20], last[M], p[M][20];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 0; i < n; ++i) cin >> s[i];
sort(s, s + n);
for(int i = 0; i < M; ++i) for(int j = 0; j < n; ++j) dp[i][j] = inf;
for(int i = 0; i < n; ++i) dp[(1 << i)][i] = n + s[i].size();
for(int mask = 1; mask < (1 << n); ++mask){
for(int last = 0; last < n; ++last) if((mask >> last) & 1){
for(int i = 0; i < n; ++i){
if((mask >> i) & 1) continue;
int to = mask + (1 << i);
if(dp[mask][last] + cost(s[last], s[i]) < dp[to][i]){
dp[to][i] = dp[mask][last] + cost(s[last], s[i]);
p[to][i] = last;
}
}
}
}
ll mn = inf, mask = (1 << n) - 1, cur;
for(int i = 0; i < n; ++i){
if(dp[mask][i] < mn){
mn = dp[mask][i];
cur = i;
}
}
cout << mn << endl;
vector <string> v;
while(mask){
ll to = mask - (1 << cur);
v.pb(s[cur]);
cur = p[mask][cur];
mask = to;
}
string have = "";
for(int i = n - 1; i >= 0; --i){
while(have.size() > s[i].size() || s[i].substr(0, (int)have.size()) != have) have.pop_back(), ans.pb('-');
for(int j = have.size(); j < s[i].size(); ++j) have += s[i][j], ans.pb(s[i][j]);
ans.pb('P');
}
for(auto i : ans) cout << i << endl;
return 0;
}
Compilation message
printer.cpp: In function 'll cost(std::string, std::string)':
printer.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
30 | for(int i = 0; i < min(s.size(), t.size()); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:80:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int j = have.size(); j < s[i].size(); ++j) have += s[i][j], ans.pb(s[i][j]);
| ~~^~~~~~~~~~~~~
printer.cpp:72:27: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
72 | ll to = mask - (1 << cur);
| ~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37836 KB |
Output is correct |
2 |
Correct |
14 ms |
37804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
37836 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
38180 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
469 ms |
58404 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
51 ms |
76552 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
76580 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
254 ms |
76704 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
699 ms |
76668 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1100 ms |
34628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1095 ms |
26308 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |