-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_2352_equalPairs.cc
More file actions
90 lines (82 loc) · 1.51 KB
/
Problem_2352_equalPairs.cc
File metadata and controls
90 lines (82 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
private:
class TireTree
{
public:
class Node
{
public:
int pass;
int end;
unordered_map<int, Node *> nexts;
Node()
{
pass = 0;
end = 0;
}
};
class Tire
{
public:
Node *root;
Tire() { root = new Node; }
};
};
public:
int equalPairs(vector<vector<int>> &grid)
{
int n = grid.size();
TireTree::Tire t;
for (int i = 0; i < n; i++)
{
TireTree::Node *cur = t.root;
for (int j = 0; j < n; j++)
{
int v = grid[i][j];
if (cur->nexts.find(v) == cur->nexts.end())
{
cur->nexts.emplace(v, new TireTree::Node);
}
cur = cur->nexts[v];
cur->pass++;
}
cur->end++;
}
int ans = 0;
for (int j = 0; j < n; j++)
{
TireTree::Node *cur = t.root;
for (int i = 0; i < n; i++)
{
int v = grid[i][j];
if (cur->nexts.find(v) == cur->nexts.end())
{
break;
}
cur = cur->nexts[v];
}
ans += cur->end;
}
return ans;
}
};
void test()
{
Solution s;
vector<vector<int>> g1 = {{3, 2, 1}, {1, 7, 6}, {2, 7, 7}};
vector<vector<int>> g2 = {{3, 1, 2, 2}, {1, 4, 4, 5}, {2, 4, 2, 2}, {2, 4, 2, 2}};
EXPECT_EQ_INT(1, s.equalPairs(g1));
EXPECT_EQ_INT(3, s.equalPairs(g2));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}