답안 #800530

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
800530 2023-08-01T15:55:05 Z fatherofnation 원형 문자열 (IZhO13_rowords) C++14
컴파일 오류
0 ms 0 KB
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int longestCommonSubsequence(const string& word1, const string& word2) {
    int m = word1.length();
    int n = word2.length();

    // Creating a 2D DP table
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (word1[i - 1] == word2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    return dp[m][n];
}

int main() {
    string word1, word2;
    cin >> word1 >> word2;

    // Find the length of the largest common subsequence between
    // roundWord1 and word2, and also between roundWord2 and word1.
    string roundWord1 = word1 + word1;
    string roundWord2 = word2 + word2;
    int result1 = longestCommonSubsequence(roundWord1, word2);
    int result2 = longestCommonSubsequence(roundWord2, word1);

    // Take the maximum of both results to handle the circular nature of round words.
    int result = min(result1, result2);

    cout << result << endl;

    return 0;
}

Compilation message

rowords.cpp: In function 'int longestCommonSubsequence(const string&, const string&)':
rowords.cpp:12:5: error: 'vector' was not declared in this scope
   12 |     vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
      |     ^~~~~~
rowords.cpp:4:1: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?
    3 | #include <algorithm>
  +++ |+#include <vector>
    4 | 
rowords.cpp:12:19: error: expected primary-expression before 'int'
   12 |     vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
      |                   ^~~
rowords.cpp:17:17: error: 'dp' was not declared in this scope
   17 |                 dp[i][j] = dp[i - 1][j - 1] + 1;
      |                 ^~
rowords.cpp:19:17: error: 'dp' was not declared in this scope
   19 |                 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      |                 ^~
rowords.cpp:24:12: error: 'dp' was not declared in this scope
   24 |     return dp[m][n];
      |            ^~