提出 #15936454
ソースコード 拡げる
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <bits/stdc++.h>
using namespace std;
bool is_palindrome(const string& s) {
int n = s.size();
for (int i = 0; i < n; i++) {
if (s[i] != s[n - 1 - i]) {
return false;
}
}
return true;
}
long long ret;
map<pair<string, string>, long long> cache;
priority_queue<pair<long long, pair<string, string>>> heap;
void add_to_heap(const string& s1, const string& s2, long long cost) {
auto s = make_pair(s1, s2);
auto it = cache.find(s);
if (it != cache.end() && cost >= it->second) {
return;
}
cache[s] = cost;
heap.push(make_pair(-cost, s));
}
void add(const string& s1, const string& s2, long long cost) {
if (cost >= ret) {
return;
}
int l1 = s1.length();
int l2 = s2.length();
int l = min(l1, l2);
for (int i = 0; i < l; ++i) {
if (s1[i] != s2[l2 - 1 - i]) {
return;
}
}
if (l1 == l2) {
ret = min(ret, cost);
return;
}
if (l1 > l2) {
string s = s1.substr(l2);
if (is_palindrome(s)) {
ret = min(ret, cost);
return;
}
add_to_heap(s, "", cost);
}
else {
string s = s2.substr(0, l2 - l1);
if (is_palindrome(s)) {
ret = min(ret, cost);
return;
}
add_to_heap("", s, cost);
}
}
int main()
{
#ifdef _MSC_VER
freopen("f.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
int n;
cin >> n;
vector<string> s(n);
vector<long long> c(n);
for (int i = 0; i < n; ++i) {
cin >> s[i] >> c[i];
}
ret = (1LL << 62);
for (int i = 0; i < n; ++i) {
add(s[i], "", c[i]);
}
while (!heap.empty()) {
auto w = heap.top().second;
long long cost = -heap.top().first;
heap.pop();
if (cost != cache[w]) {
continue;
}
for (int i = 0; i < n; i++) {
if (w.first == "") {
add(w.first + s[i], w.second, cost + c[i]);
} else {
add(w.first, s[i] + w.second, cost + c[i]);
}
}
}
if (ret >= (1LL << 62)) {
ret = -1;
}
cout << ret << endl;
return 0;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | F - Making Palindrome |
| ユーザ | int65536 |
| 言語 | C++ (GCC 9.2.1) |
| 得点 | 600 |
| コード長 | 2019 Byte |
| 結果 | AC |
| 実行時間 | 10 ms |
| メモリ | 3660 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 600 / 600 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| 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, s1.txt, s2.txt, s3.txt, s4.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 01.txt | AC | 7 ms | 3524 KiB |
| 02.txt | AC | 2 ms | 3520 KiB |
| 03.txt | AC | 3 ms | 3472 KiB |
| 04.txt | AC | 3 ms | 3636 KiB |
| 05.txt | AC | 3 ms | 3540 KiB |
| 06.txt | AC | 2 ms | 3556 KiB |
| 07.txt | AC | 2 ms | 3480 KiB |
| 08.txt | AC | 4 ms | 3524 KiB |
| 09.txt | AC | 3 ms | 3516 KiB |
| 10.txt | AC | 2 ms | 3660 KiB |
| 11.txt | AC | 2 ms | 3504 KiB |
| 12.txt | AC | 2 ms | 3524 KiB |
| 13.txt | AC | 5 ms | 3628 KiB |
| 14.txt | AC | 8 ms | 3536 KiB |
| 15.txt | AC | 3 ms | 3624 KiB |
| 16.txt | AC | 3 ms | 3588 KiB |
| 17.txt | AC | 2 ms | 3588 KiB |
| 18.txt | AC | 2 ms | 3580 KiB |
| 19.txt | AC | 3 ms | 3552 KiB |
| 20.txt | AC | 3 ms | 3600 KiB |
| 21.txt | AC | 2 ms | 3544 KiB |
| 22.txt | AC | 3 ms | 3544 KiB |
| 23.txt | AC | 2 ms | 3512 KiB |
| 24.txt | AC | 2 ms | 3640 KiB |
| 25.txt | AC | 3 ms | 3560 KiB |
| 26.txt | AC | 3 ms | 3576 KiB |
| 27.txt | AC | 5 ms | 3592 KiB |
| 28.txt | AC | 2 ms | 3652 KiB |
| 29.txt | AC | 3 ms | 3544 KiB |
| 30.txt | AC | 2 ms | 3652 KiB |
| 31.txt | AC | 2 ms | 3532 KiB |
| 32.txt | AC | 3 ms | 3580 KiB |
| 33.txt | AC | 5 ms | 3576 KiB |
| 34.txt | AC | 3 ms | 3612 KiB |
| 35.txt | AC | 4 ms | 3600 KiB |
| 36.txt | AC | 3 ms | 3484 KiB |
| 37.txt | AC | 2 ms | 3488 KiB |
| 38.txt | AC | 2 ms | 3648 KiB |
| 39.txt | AC | 4 ms | 3520 KiB |
| 40.txt | AC | 4 ms | 3608 KiB |
| 41.txt | AC | 4 ms | 3588 KiB |
| 42.txt | AC | 3 ms | 3612 KiB |
| 43.txt | AC | 10 ms | 3656 KiB |
| 44.txt | AC | 7 ms | 3656 KiB |
| 45.txt | AC | 5 ms | 3560 KiB |
| 46.txt | AC | 3 ms | 3580 KiB |
| 47.txt | AC | 2 ms | 3636 KiB |
| 48.txt | AC | 3 ms | 3600 KiB |
| 49.txt | AC | 2 ms | 3472 KiB |
| 50.txt | AC | 4 ms | 3544 KiB |
| 51.txt | AC | 4 ms | 3608 KiB |
| 52.txt | AC | 4 ms | 3544 KiB |
| 53.txt | AC | 2 ms | 3500 KiB |
| 54.txt | AC | 2 ms | 3580 KiB |
| s1.txt | AC | 3 ms | 3524 KiB |
| s2.txt | AC | 3 ms | 3528 KiB |
| s3.txt | AC | 2 ms | 3456 KiB |
| s4.txt | AC | 3 ms | 3568 KiB |