ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#212014 | #3817. 写字 | Panjunnan | Compile Error | / | / | C++ | 1.2kb | 2024-10-13 10:10:47 | 2024-10-13 12:12:55 |
answer
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
int findShortestTime(const string& S, const string& T) {
int n = S.length();
int m = T.length();
unordered_map<char, vector<int>> charPositions;
for (int i = 0; i < n; i++) {
charPositions[S[i]].push_back(i);
}
int currentPos = 0;
int time = 0;
for (int i = 0; i < m; i++) {
char targetChar = T[i];
if (charPositions.find(targetChar) == charPositions.end()) {
return -1;
}
vector<int>& positions = charPositions[targetChar];
int targetPos = -1;
int minDistance = n + 1;
for (int pos : positions) {
int distance = abs(pos - currentPos);
if (distance < minDistance) {
minDistance = distance;
targetPos = pos;
}
}
if (targetPos == -1) {
return -1;
}
time += minDistance;
currentPos = targetPos;
}
return time;
}
int main() {
int n, m;
cin >> n >> m;
string S, T;
cin >> S >> T;
cout << findShortestTime(S, T) << endl;
return 0;
}
详细
In file included from /usr/include/c++/4.8/unordered_map:35:0, from answer.code:4: /usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. #error This file requires compiler and library support for the \ ^ answer.code: In function 'int findShortestTime(const string&, const string&...