UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212186#3817. 写字Panjunnan00ms1260kbC++112.6kb2024-10-13 11:55:262024-10-13 12:31:01

answer

#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
struct State {
    int index;   
    int time;    
    int tIndex; 
};

int minTimeToWrite(const string& S, const string& T) {
    int lenS = S.size();
    int lenT = T.size();
    unordered_map<char, vector<int>> charPositions;
    for (int i = 0; i < lenS; ++i) {
        charPositions[S[i]].push_back(i);
    }
    queue<State> q;
    vector<vector<bool>> visited(lenS, vector<bool>(lenT + 1, false));
    for (int i = 0; i < lenS; ++i) {
        if (S[i] == T[0]) {
            q.push({i, 0, 1}); 
            visited[i][1] = true; 
        }
    }

    while (!q.empty()) {
        State current = q.front();
        q.pop();


        if (current.tIndex == lenT) {
            return current.time;
        }

        char currentChar = S[current.index];

        if (current.index > 0 && !visited[current.index - 1][current.tIndex]) { // 左
            if (S[current.index - 1] == T[current.tIndex]) {
                visited[current.index - 1][current.tIndex] = true;
                q.push({current.index - 1, current.time + 1, current.tIndex + 1});
            } else {
                visited[current.index - 1][current.tIndex] = true;
                q.push({current.index - 1, current.time + 1, current.tIndex});
            }
        }
        if (current.index < lenS - 1 && !visited[current.index + 1][current.tIndex]) { // 右
            if (S[current.index + 1] == T[current.tIndex]) {
                visited[current.index + 1][current.tIndex] = true;
                q.push({current.index + 1, current.time + 1, current.tIndex + 1});
            } else {
                visited[current.index + 1][current.tIndex] = true;
                q.push({current.index + 1, current.time + 1, current.tIndex});
            }
        }
        for (int pos : charPositions[currentChar]) {
            if (pos != current.index && !visited[pos][current.tIndex]) {
                if (pos < current.index) {
                    visited[pos][current.tIndex] = true;
                    q.push({pos, current.time + (current.index - pos), current.tIndex});
                } else {
                    visited[pos][current.tIndex] = true;
                    q.push({pos, current.time + (pos - current.index), current.tIndex});
                }
            }
        }
    }
    return -1;
}

int main() {
    string S, T;
    cin >> S;
    cin >> T;
    int result = minTimeToWrite(S, T);
    cout << result << endl;
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 20
Accepted
time: 0ms
memory: 1244kb

input:

1 1
v
v

output:

0

result:

ok 1 number(s): "0"

Test #2:

score: -20
Wrong Answer
time: 0ms
memory: 1244kb

input:

5 5
bacbb
cabcb

output:

0

result:

wrong answer 1st numbers differ - expected: '7', found: '0'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 1248kb

input:

26 300
ywzhvjnpdfqtukimsrbxageloc
brsmsrbxbxbxbxagagagegelococololegaxagaxaxbxbrbxbrsrsmikimikikimim...

output:

-1

result:

wrong answer 1st numbers differ - expected: '299', found: '-1'

Subtask #3:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 0ms
memory: 1260kb

input:

300 300
hgbfdbgcghedefchdabhgdddahcdedebceffegfbceehceeheggffhhddbecbfdhceeedcaeeebdaddfgccggfdcachg...

output:

2

result:

wrong answer 1st numbers differ - expected: '3643', found: '2'