First question:
Basic questions, if the data size is the same as the description of the questions, it's better to be violent
#include<iostream> using namespace std; int main() { int n, ans; while (cin >> n) { ans = 1; for (int i = 1; i <= n; i++) ans *= i; cout << ans << endl; } return 0; }
Second question:
Generally, questions matching brackets can be simulated by stack, which is more convenient (personal opinion)
#include<iostream> #include<algorithm> #include<string> #include<stack> using namespace std; int main() { string s; stack<char>my_stack; int ans = 1; while (cin >> s) { ans = 1; while (!my_stack.empty()) my_stack.pop(); for (int i = 0; i < s.size(); i++) { if (s[i] == '{' || s[i] == '[' || s[i] == '<' || s[i] == '(') my_stack.push(s[i]); else { if (s[i] == ')' && my_stack.top() != '(') ans = 0; else if (s[i] == '}' && my_stack.top() != '{') ans = 0; else if (s[i] == '>' && my_stack.top() != '<') ans = 0; else if (s[i] == ']' && my_stack.top() != '[') ans = 0; else my_stack.pop(); } } if (my_stack.size() != 0) ans = 0; if (ans == 0)cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
Third question:
I'm sorry, I forgot to add a sentence in the previous version. The main thing is to add the minimum spanning tree and look up the set
#include<iostream> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef struct Edge{ int u, v; int w; Edge(int a, int b, int c) : u(a), v(b), w(c) {} }edge; const int MAXN = 1010; vector<edge>G; int father[MAXN]; int find(int x) { if (father[x] == x) return x; else { int F = find(father[x]); father[x] = F; return F; } } bool cmp(edge a, edge b) { return a.w < b.w; } int main() { int k, n, m; int a, b, c; int cnt, index, ans; cin >> k; while (k--) { cin >> n >> m; G.clear(); for (int i = 0; i < n; i++) father[i] = i; for (int i = 0; i < m; i++) { cin >> a >> b >> c; G.push_back(Edge(a, b, c)); } sort(G.begin(), G.end(), cmp); cnt = n - 1, index = 0, ans = 0; while(cnt){ if (find(G[index].u) != find(G[index].v)) { ans += G[index].w; father[find(G[index].u)] = find(G[index].v);//The most important sentence forgotten before cnt--; } index++; } cout << ans << endl; } return 0; }
Fourth question:
This problem can be realized by DP and stack operation
#include<iostream> #include<algorithm> #include<cmath> #include<vector> #include<stack> using namespace std; int a[510]; stack<int>my_stack; int main() { int n, ans; while (cin >> n) { ans = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { while (!my_stack.empty() && a[i] <= my_stack.top()) my_stack.pop(); my_stack.push(a[i]); ans = max(ans, (int)my_stack.size()); } cout << ans << endl; } return 0; }
Fifth question:
The root node is the only path to each node (if not repeated), so as long as breadth first search or depth first search to find the farthest point, it is OK
#include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; const int MAXN = 10010; typedef struct Edge { int v, w; Edge(int a, int b) : v(a), w(b) {} }edge; typedef struct Node { int v, high; Node(int a, int b) : v(a), high(b) {} }node; vector<vector<edge>>G(MAXN); vector<int>vis(MAXN); queue<node>Q; int main() { int n, m; int a, b; int ans = 0; while (cin >> n >> m) { for (int i = 0; i <= n;i++) G[i].clear(); for (int i = 0; i <= n; i++) vis[i] = 0; for (int i = 0; i < n - 1; i++) { cin >> a >> b; G[a].push_back(Edge(b, 1)); G[b].push_back(Edge(a, 1)); } vis[m] = 1; for (int i = 0; i < G[m].size(); i++) { if (vis[G[m][i].v] == 0) { Q.push(Node(G[m][i].v,1)); vis[G[m][i].v] = 1; } } while (!Q.empty()) { int x = Q.front().v; for (int i = 0; i < G[x].size(); i++) { if (vis[G[x][i].v] == 0) { ans = max(ans, Q.front().high + 1); Q.push(Node(G[x][i].v, Q.front().high + 1)); vis[G[x][i].v] = 1; } } Q.pop(); } cout << ans << endl; } return 0; }
Sixth question:
Simple simulation questions
#include<iostream> #include<algorithm> #include<string> using namespace std; string s, s1 = "MF"; void my_replace() { int len = s.size(), i = 0; while (i < len) { if (s[i] == 'M' && s[i + 1] == 'F') { s[i] = 'F'; s[i + 1] = 'M'; i += 2; } else i++; } } int main() { int ans; while (cin >> s) { ans = 0; while (s.find(s1) != string::npos) { ans++; my_replace(); } cout << ans << endl; } return 0; }