Altered Algorithm
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
//确定每个节点要干什么
|
||||
bool dfs(const string& s, int l, int r) {
|
||||
if (l > r) return true; // 空串平衡
|
||||
if (l == r) return false; // 单个字符不平衡
|
||||
|
||||
// 必须匹配类型
|
||||
char open = s[l];
|
||||
char close = (open == '(') ? ')' : (open == '[') ? ']' : 0;
|
||||
if (close == 0) return false; // 非括号字符
|
||||
|
||||
int cnt = 0;
|
||||
int p = -1;
|
||||
for (int i = l; i <= r; i++) {
|
||||
if (s[i] == '(' || s[i] == '[') cnt++;
|
||||
if (s[i] == ')' || s[i] == ']') cnt--;
|
||||
if (cnt == 0) {
|
||||
p = i; // 找到匹配的右括号
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (p == -1) return false; // 没找到匹配
|
||||
if (s[p] != close) return false; // 类型不匹配(第一个和最后一个字符匹配)
|
||||
|
||||
// 内部和剩余区间递归
|
||||
return dfs(s, l + 1, p - 1) && dfs(s, p + 1, r);
|
||||
}
|
||||
|
||||
int main(){
|
||||
string a;
|
||||
while(true)
|
||||
{
|
||||
cin >> a;
|
||||
dfs(a, 0, a.size() - 1) ? cout << "YES" << endl : cout << "NO" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
108
Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp
Normal file
108
Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
vector<int> result;
|
||||
|
||||
int _5to10(const vector<int>& x){
|
||||
int num = 0;
|
||||
for(int i = x.size() - 1; i >= 0; i--){
|
||||
num = num * 5 + x[i];
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
//Tȡbool<6F><6C><EFBFBD><EFBFBD>int[0, 1]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBC>дһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
template <class T>
|
||||
struct Node{
|
||||
T element;
|
||||
bool isLeaf;
|
||||
Node<T>* NW;//1
|
||||
Node<T>* NE;//2
|
||||
Node<T>* SW;//3
|
||||
Node<T>* SE;//4
|
||||
vector<int> path;
|
||||
|
||||
Node(const T& _element) : isLeaf(true), element(_element), NW(nullptr), NE(nullptr), SW(nullptr), SE(nullptr) {}
|
||||
};
|
||||
|
||||
//<2F><><EFBFBD>ﲻ<EFBFBD><EFB2BB>ҪTree<65><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>õݹ<C3B5><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD>Խ<EFBFBD><D4BD>ߵ<EFBFBD>(i, j)
|
||||
template <class T>
|
||||
void dfs(vector<vector<int>>& mat, Node<T>*& node, vector<int>& path, int li, int lj, int ri, int rj) {
|
||||
int val = mat[li][lj];
|
||||
bool same = true;
|
||||
for (int i = li; i <= ri && same; i++) {
|
||||
for (int j = lj; j <= rj && same; j++) {
|
||||
if (mat[i][j] != val) same = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (same) { // Ҷ<><D2B6>
|
||||
node = new Node<T>(val);
|
||||
node->path = path;
|
||||
|
||||
//ת<><D7AA>10<31><30><EFBFBD><EFBFBD>
|
||||
result.push_back(_5to10(node->path));
|
||||
return;
|
||||
}
|
||||
|
||||
node = new Node<T>(-1); // <20>ڲ<EFBFBD><DAB2>ڵ<EFBFBD>
|
||||
node->isLeaf = false;
|
||||
|
||||
int midRow = (li + ri) / 2;
|
||||
int midCol = (lj + rj) / 2;
|
||||
|
||||
// NW=1
|
||||
path.push_back(1);
|
||||
dfs(mat, node->NW, path, li, lj, midRow, midCol);
|
||||
path.pop_back();
|
||||
|
||||
// NE=2
|
||||
path.push_back(2);
|
||||
dfs(mat, node->NE, path, li, midCol+1, midRow, rj);
|
||||
path.pop_back();
|
||||
|
||||
// SW=3
|
||||
path.push_back(3);
|
||||
dfs(mat, node->SW, path, midRow+1, lj, ri, midCol);
|
||||
path.pop_back();
|
||||
|
||||
// SE=4
|
||||
path.push_back(4);
|
||||
dfs(mat, node->SE, path, midRow+1, midCol+1, ri, rj);
|
||||
path.pop_back();
|
||||
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
vector<vector<int>> matrix(n, vector<int>(m));
|
||||
for(int i = 0; i < n; i++)
|
||||
for(int j = 0; j < m; j++)
|
||||
cin >> matrix[i][j];
|
||||
|
||||
Node<int>* root = nullptr;
|
||||
vector<int> path;
|
||||
dfs(matrix, root, path, 0, 0, n-1, m-1);
|
||||
|
||||
sort(result.begin(), result.end());
|
||||
for (int v : result) cout << v << " ";
|
||||
// TODO: <20><><EFBFBD><EFBFBD>Ҷ<EFBFBD>ӽڵ㣬<DAB5><E3A3AC> path ת<><D7AA>Ϊʮ<CEAA><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
8 8
|
||||
0 0 0 0 0 0 0 0
|
||||
0 0 0 0 0 0 0 0
|
||||
0 0 0 0 1 1 1 1
|
||||
0 0 0 0 1 1 1 1
|
||||
0 0 0 1 1 1 1 1
|
||||
0 0 1 1 1 1 1 1
|
||||
0 0 1 1 1 1 0 0
|
||||
0 0 1 1 1 0 0 0
|
||||
1 7 8 9 12 14 17 18 22 23 24 38 44 63 69 88 94 113 119
|
||||
|
||||
*/
|
Reference in New Issue
Block a user