與2A題的概念相當,不過目的是在於將「所有的組合」都給生成出來。
可以利用簡單的DFS,將每個狀況皆列舉出來後,再判斷當前組合是否無法形成括弧組合的方式。
# include <bits/stdc++.h>
using namespace std;
void paren(int n, int l, string s){
if(l<0 || n<0)
return;
if(n == 0 && l == 0){
cout<<s<<'\n';
return;
}
paren(n-1, l+1, s+'(');
paren(n-1, l-1, s+')');
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
while(cin>>n){
paren(n*2,0,"");
cout<<'\n';
}
}
C. 完美平方數 #include <iostream> #include <math.h> using namespace std; int numSquares(int n); int main() { int num, ans;
Jan 27, 2022E. 鐵路 本題源自於Onling Judge:514 - Rails 題目目標在於給定出站順序的前提下,利用已知入站順序1~N, 確定是否仍能夠以目標出站順序離開。 有一個簡單的想法,我們利用queue的特性來維護出站順序、 利用stack的特性來維護入站順序, 並依次比較它們的front/top是否相同,如果相同就安排出站(pop)。
Jan 27, 2022C. 組合 #include<bits/stdc++.h> using namespace std; vector<int> a; bool first=true,f=true; fstream input,output; void find(int g,vector<int> &can,int p){ if(!g){ if(!first){
Sep 19, 2021D. Flood Fill # include <bits/stdc++.h> using namespace std; int m[102][102]; struct Fill{ int x,y,t; };
Sep 16, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up