8.25深信服笔试C++
深信服字符串变换代码如下,为啥子不过呢,每次都对前一次的0 - 9索引下的转变结果进行修改,没想明白为什么没过
#include<iostream> #include<vector> #include<string> #include<map> using namespace std; class Solution{ public: void solve(string str, vector<pair<int, int> >&nums){ int size = nums.size(); map<int, int>M; for(int i = 0; i < size; i++){ int a = nums[i].first, b = nums[i].second; if(M.find(a) == M.end()){ M[a] = b; for(int i = 0; i < 10; i++){ if(M.find(i) != M.end() && M[i] == a)M[i] = b; } } } for(int i = 0; i <str.length(); i++){ int tmp = str[i] - '0'; if(M.find(tmp) != M.end()) str[i] = M[tmp] + '0'; } cout << str; return; } }; int main(){ string str = ""; cin >> str; int n; cin >> n; vector<pair<int, int> >nums(n, pair<int, int>(0, 0)); for(int i = 0; i < n; i++){ int a, b; cin >> a >> b; nums[i].first = a; nums[i].second = b; } Solution s; s.solve(str, nums); cout<<endl; return 0; }