結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー | SSRS |
提出日時 | 2021-07-21 21:58:59 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,646 bytes |
コンパイル時間 | 2,133 ms |
コンパイル使用メモリ | 191,448 KB |
実行使用メモリ | 62,528 KB |
最終ジャッジ日時 | 2024-07-17 18:17:01 |
合計ジャッジ時間 | 8,392 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 191 ms
58,436 KB |
testcase_07 | AC | 201 ms
58,504 KB |
testcase_08 | AC | 176 ms
58,508 KB |
testcase_09 | AC | 146 ms
58,572 KB |
testcase_10 | AC | 139 ms
58,504 KB |
testcase_11 | AC | 144 ms
58,564 KB |
testcase_12 | AC | 153 ms
58,820 KB |
testcase_13 | AC | 154 ms
58,940 KB |
testcase_14 | AC | 150 ms
58,820 KB |
testcase_15 | AC | 251 ms
62,408 KB |
testcase_16 | AC | 244 ms
62,528 KB |
testcase_17 | AC | 248 ms
62,400 KB |
testcase_18 | AC | 3 ms
6,940 KB |
testcase_19 | AC | 3 ms
6,944 KB |
testcase_20 | AC | 3 ms
6,944 KB |
testcase_21 | AC | 3 ms
6,940 KB |
testcase_22 | AC | 3 ms
6,940 KB |
testcase_23 | AC | 3 ms
6,944 KB |
testcase_24 | AC | 4 ms
6,944 KB |
testcase_25 | AC | 4 ms
6,940 KB |
testcase_26 | AC | 4 ms
6,940 KB |
testcase_27 | AC | 2 ms
6,940 KB |
testcase_28 | AC | 2 ms
6,944 KB |
testcase_29 | AC | 2 ms
6,940 KB |
testcase_30 | AC | 2 ms
6,944 KB |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 2 ms
6,940 KB |
testcase_34 | AC | 2 ms
6,940 KB |
testcase_35 | AC | 2 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,940 KB |
testcase_37 | AC | 2 ms
6,944 KB |
testcase_38 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; vector<int> z_algorithm(string S){ int N = S.size(); vector<int> ans(N); for (int i = 1, j = 0; i < N; i++){ if (i + ans[i - j] < j + ans[j]){ ans[i] = ans[i - j]; } else { int k = max(0, j + ans[j] - i); while (i + k < N && S[k] == S[i + k]){ k++; } ans[i] = k; j = i; } } ans[0] = N; return ans; } int main(){ int N, M; cin >> N >> M; vector<vector<tuple<char, int, int>>> E(N); for (int i = 0; i < M; i++){ int A, B; char C; cin >> A >> B >> C; A--; B--; E[A].push_back(make_tuple(C, i, B)); E[B].push_back(make_tuple(C, i, A)); } vector<vector<int>> d(27, vector<int>(N, -1)); vector<vector<int>> pv(27, vector<int>(N, -1)); vector<vector<int>> pe(27, vector<int>(N, -1)); vector<vector<int>> ps(27, vector<int>(N, -1)); vector<vector<char>> pc(27, vector<char>(N, -1)); queue<pair<int, int>> Q; for (int i = 0; i < 26; i++){ d[i][0] = 0; Q.push(make_pair(i, 0)); } while (!Q.empty()){ int s = Q.front().first; int v = Q.front().second; Q.pop(); for (auto e : E[v]){ char c = get<0>(e); if (!(v == 0 && s != c - 'a')){ int w = get<2>(e); int s2 = s; if (s != c - 'a'){ s2 = 26; } if (d[s2][w] == -1){ d[s2][w] = d[s][v] + 1; pv[s2][w] = v; pe[s2][w] = get<1>(e); ps[s2][w] = s; pc[s2][w] = c; Q.push(make_pair(s2, w)); } } } } if (d[26][N - 1] == -1){ cout << -1 << endl; } else { string S; vector<int> ans; int c = N - 1; int s = 26; while (c != 0 || s == 26){ S += pc[s][c]; ans.push_back(pe[s][c]); int c2 = pv[s][c]; int s2 = ps[s][c]; c = c2; s = s2; } reverse(S.begin(), S.end()); reverse(ans.begin(), ans.end()); int cnt = 0; while (S.size() + 2 <= N * 2){ char ch = pc[26][N - 1]; S += ch; S += ch; ans.push_back(pe[26][N - 1]); ans.push_back(pe[26][N - 1]); cnt += 2; } string S2 = S; reverse(S2.begin(), S2.end()); string T = S + '$' + S2; vector<int> Z = z_algorithm(T); int p = -1; for (int i = 0; i <= cnt; i += 2){ if (Z[S.size() + 1 + i] != S.size() - i){ p = i; break; } } if (p == -1){ cout << -1 << endl; } else { for (int i = 0; i < cnt - p; i++){ ans.pop_back(); } int k = ans.size(); cout << k << endl; for (int i = 0; i < k; i++){ cout << ans[i] + 1 << endl; } } } }