ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#212156 | #3817. 写字 | yangtianming001 | 0 | 0ms | 1320kb | C++11 | 1.9kb | 2024-10-13 11:39:06 | 2024-10-13 12:28:08 |
answer
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <climits>
#include <tuple>
using namespace std;
int find_min_time(string S, string T) {
int n = S.length(), m = T.length();
unordered_map<char, vector<int>> char_pos;
for (int i = 0; i < n; i++) {
char_pos[S[i]].push_back(i);
}
for (char c : T) {
if (char_pos.find(c) == char_pos.end()) {
return -1;
}
}
vector<vector<int>> dp(n, vector<int>(m, INT_MAX));
queue<tuple<int, int, int>> q;
for (int start_pos : char_pos[T[0]]) {
q.push(make_tuple(start_pos, 0, 1));
dp[start_pos][0] = 1;
}
while (!q.empty()) {
tuple<int, int, int> front = q.front();
q.pop();
int s_pos = get<0>(front);
int t_pos = get<1>(front);
int time = get<2>(front);
if (t_pos == m - 1) {
return time;
}
char next_char = T[t_pos + 1];
if (s_pos - 1 >= 0 && dp[s_pos - 1][t_pos] > time + 1) {
dp[s_pos - 1][t_pos] = time + 1;
q.push(make_tuple(s_pos - 1, t_pos, time + 1));
}
if (s_pos + 1 < n && dp[s_pos + 1][t_pos] > time + 1) {
dp[s_pos + 1][t_pos] = time + 1;
q.push(make_tuple(s_pos + 1, t_pos, time + 1));
}
for (int jump_pos : char_pos[next_char]) {
if (jump_pos != s_pos && dp[jump_pos][t_pos + 1] > time + abs(jump_pos - s_pos)) {
dp[jump_pos][t_pos + 1] = time + abs(jump_pos - s_pos);
q.push(make_tuple(jump_pos, t_pos + 1, dp[jump_pos][t_pos + 1]));
}
}
}
return -1;
}
int main() {
int n, m;
string S, T;
cin >> n >> m;
cin >> S;
cin >> T;
cout << find_min_time(S, T) << endl;
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 1276kb
input:
1 1 v v
output:
1
result:
wrong answer 1st numbers differ - expected: '0', found: '1'
Subtask #2:
score: 0
Wrong Answer
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 1320kb
input:
26 300 ywzhvjnpdfqtukimsrbxageloc brsmsrbxbxbxbxagagagegelococololegaxagaxaxbxbrbxbrsrsmikimikikimim...
output:
300
result:
wrong answer 1st numbers differ - expected: '299', found: '300'
Subtask #3:
score: 0
Time Limit Exceeded
Test #6:
score: 0
Time Limit Exceeded
input:
300 300 hgbfdbgcghedefchdabhgdddahcdedebceffegfbceehceeheggffhhddbecbfdhceeedcaeeebdaddfgccggfdcachg...