正文
增加 22 行删除 22 行
31 行未改动323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015131 行未改动map<string, int> parse_formula(const string& s) {map<string, int> elem_count;int i = 0;int n = s.length();while (i < n) {int j = i;while (j < n && isalpha(s[j])) {j++;}string elem = s.substr(i, j - i);int k = j;while (k < n && isdigit(s[k])) {k++;}int cnt = 1;if (k > j) {cnt = stoi(s.substr(j, k - j));}elem_count[elem] = cnt;i = k;}return elem_count;}int matrix_rank(vector<vector<double>>& A) {int m = A.size();int n = A[0].size();int row = 0;vector<vector<double>> M = A;for (int col = 0; col < n; col++) {int max_row = row;for (int r = row; r < m; r++) {if (fabs(M[r][col]) > fabs(M[max_row][col])) {max_row = r;}}if (fabs(M[max_row][col]) < 1e-8) {continue;}if (max_row != row) {swap(M[row], M[max_row]);}for (int r = row + 1; r < m; r++) {double factor = M[r][col] / M[row][col];for (int c = col; c < n; c++) {M[r][c] -= factor * M[row][c];}}row++;if (row == m) {break;}}return row;}int main() {int n;cin >> n;cin.ignore();for (int t = 0; t < n; t++) {string line;getline(cin, line);stringstream ss(line);int m;ss >> m;vector<string> formulas(m);for (int i = 0; i < m; i++) {ss >> formulas[i];}map<string, int> elem_to_index;for (const string& formula : formulas) {map<string, int> cnt = parse_formula(formula);for (const auto& pair : cnt) {if (elem_to_index.find(pair.first) == elem_to_index.end()) {elem_to_index[pair.first] = elem_to_index.size();}}}int rows = elem_to_index.size();int cols = m;vector<vector<double>> mat(rows, vector<double>(cols, 0.0));for (int j = 0; j < m; j++) {map<string, int> cnt = parse_formula(formulas[j]);for (const auto& pair : cnt) {int i = elem_to_index[pair.first];mat[i][j] = pair.second;}}int rank = matrix_rank(mat);if (rank < m) {cout << "Y" << endl;} else {cout << "N" << endl;}}return 0;}31 行未改动323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015131 行未改动map<string, int> parse_formula(const string& s) {map<string, int> elem_count;int i = 0;int n = s.length();while (i < n) {int j = i;while (j < n && isalpha(s[j])) {j++;}string elem = s.substr(i, j - i);int k = j;while (k < n && isdigit(s[k])) {k++;}int cnt = 1;if (k > j) {cnt = stoi(s.substr(j, k - j));}elem_count[elem] = cnt;i = k;}return elem_count;}int matrix_rank(vector<vector<double>>& A) {int m = A.size();int n = A[0].size();int row = 0;vector<vector<double>> M = A;for (int col = 0; col < n; col++) {int max_row = row;for (int r = row; r < m; r++) {if (fabs(M[r][col]) > fabs(M[max_row][col])) {max_row = r;}}if (fabs(M[max_row][col]) < 1e-8) {continue;}if (max_row != row) {swap(M[row], M[max_row]);}for (int r = row + 1; r < m; r++) {double factor = M[r][col] / M[row][col];for (int c = col; c < n; c++) {M[r][c] -= factor * M[row][c];}}row++;if (row == m) {break;}}return row;}int main() {int n;cin >> n;cin.ignore();for (int t = 0; t < n; t++) {string line;getline(cin, line);stringstream ss(line);int m;ss >> m;vector<string> formulas(m);for (int i = 0; i < m; i++) {ss >> formulas[i];}map<string, int> elem_to_index;for (const string& formula : formulas) {map<string, int> cnt = parse_formula(formula);for (const auto& pair : cnt) {if (elem_to_index.find(pair.first) == elem_to_index.end()) {elem_to_index[pair.first] = elem_to_index.size();}}}int rows = elem_to_index.size();int cols = m;vector<vector<double>> mat(rows, vector<double>(cols, 0.0));for (int j = 0; j < m; j++) {map<string, int> cnt = parse_formula(formulas[j]);for (const auto& pair : cnt) {int i = elem_to_index[pair.first];mat[i][j] = pair.second;}}int rank = matrix_rank(mat);if (rank < m) {cout << "Y" << endl;} else {cout << "N" << endl;}}return 0;}