Submission #16280049
Source Code Expand
#include<bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < (x); i++)
#define chmin(x,y) do { x = min((ll)x, (ll)y); }while(0)
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;
const ll INF = 1e18;
ll N;
map<string, ll> m;
vector<ll> c;
vector<string> s;
vector<pair<ll,string>> nxt(string remain) {
ll cost = m[remain];
vector<pair<ll,string>> res;
if (remain.size() == 0) {
return res;
}
//cout << "base: " << cost << " " << remain << endl;
rep(i, N) {
string L, R;
if (remain[0] == '^') {
L = remain.substr(1);
R = s[i];
} else {
L = s[i];
R = remain.substr(0, remain.size() - 1);
}
ll l = min(L.size(), R.size());
bool ok = true;
rep(j, l) {
if (L[j] != R[R.size() - 1 - j]) ok = false;
}
if (!ok) continue;
if (L.size() > R.size()) res.emplace_back(cost + c[i], "^" + L.substr(l));
else res.emplace_back(cost + c[i], R.substr(0, R.size()-l) + "$");
}
for (auto &e : res) {
if (e.second == "^" || e.second == "$") e.second = "";
//cout << "nxt: " << e.first << " " << e.second << endl;
}
return res;
}
int main(void) {
cin >> N;
c.resize(N);
s.resize(N);
rep(i, N)
cin >> s[i] >> c[i];
priority_queue<pair<ll,string>, vector<pair<ll,string>>, greater<pair<ll,string>>> q;
rep(i, N) {
q.emplace(c[i], "^" + s[i]);
}
while (!q.empty()) {
ll cost = q.top().first;
string remain = q.top().second;
q.pop();
if (m.find(remain) == m.end()) m[remain] = INF;
if (m[remain] <= cost) continue;
m[remain] = cost;
for (auto e : nxt(remain)) {
q.emplace(e.first, e.second);
}
}
ll ans = INF;
for (auto e : m) {
string str = e.first;
if (str.size() > 0 && str[0] == '^') str = str.substr(1);
if (str.size() > 0 && str[str.size()-1] == '$') str = str.substr(0, str.size()-1);
bool ok = true;
rep(i, str.size()) {
if (str[i] != str[str.size()-1-i]) ok = false;
}
if (!ok) continue;
chmin(ans, e.second);
}
if (ans == INF) ans = -1;
cout << ans << endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Making Palindrome |
| User | bobuhiro11 |
| Language | C++ (GCC 9.2.1) |
| Score | 600 |
| Code Size | 2219 Byte |
| Status | AC |
| Exec Time | 14 ms |
| Memory | 3744 KiB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:2:36: warning: comparison of integer expressions of different signedness: ‘ll’ {aka ‘long long int’} and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
2 | #define rep(i, x) for (ll i = 0; i < (x); i++)
| ^
./Main.cpp:78:5: note: in expansion of macro ‘rep’
78 | rep(i, str.size()) {
| ^~~
Judge Result
| Set Name | Sample | All | after_contest | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 600 / 600 | 0 / 0 | ||||||
| Status |
|
|
|
| Set Name | Test Cases |
|---|---|
| Sample | s1.txt, s2.txt, s3.txt, s4.txt |
| All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, after_contest_01.txt, s1.txt, s2.txt, s3.txt, s4.txt |
| after_contest | after_contest_01.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01.txt | AC | 7 ms | 3556 KiB |
| 02.txt | AC | 3 ms | 3608 KiB |
| 03.txt | AC | 7 ms | 3616 KiB |
| 04.txt | AC | 3 ms | 3508 KiB |
| 05.txt | AC | 2 ms | 3492 KiB |
| 06.txt | AC | 2 ms | 3616 KiB |
| 07.txt | AC | 2 ms | 3488 KiB |
| 08.txt | AC | 4 ms | 3564 KiB |
| 09.txt | AC | 2 ms | 3492 KiB |
| 10.txt | AC | 3 ms | 3612 KiB |
| 11.txt | AC | 2 ms | 3480 KiB |
| 12.txt | AC | 2 ms | 3492 KiB |
| 13.txt | AC | 3 ms | 3480 KiB |
| 14.txt | AC | 2 ms | 3540 KiB |
| 15.txt | AC | 2 ms | 3636 KiB |
| 16.txt | AC | 2 ms | 3616 KiB |
| 17.txt | AC | 2 ms | 3496 KiB |
| 18.txt | AC | 2 ms | 3552 KiB |
| 19.txt | AC | 2 ms | 3640 KiB |
| 20.txt | AC | 3 ms | 3592 KiB |
| 21.txt | AC | 2 ms | 3624 KiB |
| 22.txt | AC | 2 ms | 3444 KiB |
| 23.txt | AC | 2 ms | 3636 KiB |
| 24.txt | AC | 5 ms | 3624 KiB |
| 25.txt | AC | 2 ms | 3624 KiB |
| 26.txt | AC | 2 ms | 3604 KiB |
| 27.txt | AC | 4 ms | 3628 KiB |
| 28.txt | AC | 3 ms | 3616 KiB |
| 29.txt | AC | 3 ms | 3612 KiB |
| 30.txt | AC | 3 ms | 3580 KiB |
| 31.txt | AC | 3 ms | 3628 KiB |
| 32.txt | AC | 3 ms | 3624 KiB |
| 33.txt | AC | 3 ms | 3496 KiB |
| 34.txt | AC | 3 ms | 3644 KiB |
| 35.txt | AC | 4 ms | 3612 KiB |
| 36.txt | AC | 3 ms | 3568 KiB |
| 37.txt | AC | 3 ms | 3584 KiB |
| 38.txt | AC | 4 ms | 3536 KiB |
| 39.txt | AC | 4 ms | 3516 KiB |
| 40.txt | AC | 4 ms | 3572 KiB |
| 41.txt | AC | 7 ms | 3608 KiB |
| 42.txt | AC | 3 ms | 3496 KiB |
| 43.txt | AC | 14 ms | 3588 KiB |
| 44.txt | AC | 9 ms | 3744 KiB |
| 45.txt | AC | 2 ms | 3508 KiB |
| 46.txt | AC | 2 ms | 3644 KiB |
| 47.txt | AC | 2 ms | 3568 KiB |
| 48.txt | AC | 2 ms | 3648 KiB |
| 49.txt | AC | 4 ms | 3496 KiB |
| 50.txt | AC | 3 ms | 3568 KiB |
| 51.txt | AC | 2 ms | 3556 KiB |
| 52.txt | AC | 2 ms | 3628 KiB |
| 53.txt | AC | 2 ms | 3612 KiB |
| 54.txt | AC | 2 ms | 3608 KiB |
| after_contest_01.txt | AC | 2 ms | 3564 KiB |
| s1.txt | AC | 2 ms | 3640 KiB |
| s2.txt | AC | 2 ms | 3492 KiB |
| s3.txt | AC | 2 ms | 3584 KiB |
| s4.txt | AC | 2 ms | 3596 KiB |