classSolution { public: string decodeCiphertext(string str, int row){ int len = str.size(); int col = len / row; vector<vector<char>> mat(row, vector<char>(col, ' ')); int x = 0, y = 0; for (auto& c : str) { mat[x][y] = c; y ++ ; if (y == col) y = 0, x ++ ; } string ans; for (int i = 0; i < col; i ++ ) { int x = 0, y = i; while (x < row and y < col) { ans.push_back(mat[x][y]); x ++, y ++; } } while (ans.size() and ans.back() == ' ') ans.pop_back(); return ans; } };
constint N = 1010; int p[N]; classSolution { public: intfind(int x){ return x == p[x] ? x : p[x] = find(p[x]); } vector<bool> friendRequests(int n, vector<vector<int>>& edge, vector<vector<int>>& qu){ int m = edge.size(); for (int i = 0; i < n; i ++ ) p[i] = i; vector<bool> ans; for (auto& q : qu) { int x = q[0], y = q[1]; x = find(x), y = find(y); if (x == y) { ans.push_back(true); continue; } // x != y unordered_set<int> sx, sy; for (int i = 0; i < n; i ++ ) { if (find(i) == x) sx.insert(i); if (find(i) == y) sy.insert(i); } bool flag = true; for (auto& e : edge) { int u = e[0], v = e[1]; if ((sx.count(u) and sy.count(v)) or (sx.count(v) and sy.count(u))) flag = false; } ans.push_back(flag); if (flag) p[y] = x; } return ans; } };