9.10 B站后台笔试题
第二题超时,自己的做法不对就不贴了,一三两题都过了
第一题:leetcode原题,之前做过
#include <bits/stdc++.h> using namespace std; int main() { string word1; string word2; while (cin >> word1 >> word2) { int n1 = word1.size(); int n2 = word2.size(); vector<vector<int>> dp(n2 + 1, vector<int>(n1 + 1)); for (int i = 0; i<n2; i++) { dp[i][0] = i; } for (int i = 0; i<n1; i++) { dp[0][i] = i; } for (int i = 1; i <= n2; i++) { for (int j = 1; j <= n1; j++) { if (word1[j] == word2[i]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = min(min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]) + 1; } } } cout << dp[n2][n1] << endl; } return 0; }第三题:会用getline和stringstream基本都能做出来
#include <bits/stdc++.h> #include<string> using namespace std; int main() { string str; while (getline(cin, str)) { char pair_deli; char key_value_deli; int n = str.length(); if (n <= 4) { cout << 0 << endl; } else { pair_deli = str[0]; key_value_deli = str[2]; string str1 = str.substr(4); stringstream split1(str1); vector<string> v; string pair; while (getline(split1, pair, pair_deli)) { v.push_back(pair); } int size = v.size(); vector<string> ret; for (int i = 0; i < size; i++) { stringstream split(v[i]); string ret1; string ret2; getline(split, ret1, key_value_deli); getline(split, ret2, key_value_deli); if (ret1.length() == 0 || ret2.length() == 0) { continue; } else { string retstr = ret1 + " " + ret2; ret.push_back(retstr); } } int count = ret.size(); cout << count << endl; for (int i = 0; i < count; i++) { cout << ret[i] << endl; } } } return 0; }