# hihoCoder 1552-缺失的拼图

hihoCoder 1552-缺失的拼图

### 输出

```5
0 0 4 5
0 0 3 1
0 1 2 5
3 0 4 5
2 2 3 5```

`2 1 3 2`

```#include<algorithm>
#include<vector>
#include<iostream>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> Point;
int main() {
//freopen("input.txt", "r", stdin);
int n;
scanf("%d", &n);
map<Point, int> hash;
int a, b, c, d;
while (n--) {
Point left_bottom, right_top;
scanf("%d %d %d %d", &left_bottom.first, &left_bottom.second, &right_top.first, &right_top.second);
Point left_top = Point(left_bottom.first, right_top.second), right_bottom = Point(right_top.first, left_bottom.second);
++hash[left_bottom];
++hash[right_top];
++hash[left_top];
++hash[right_bottom];
}
set<Point> ans;
for (auto p : hash) {
if (p.second % 2 == 1) {
ans.insert(p.first);
}
}
a = c = ans.begin()->first;
b = d = ans.begin()->second;
for (auto p : ans) {
a = min(a, p.first);
b = min(b, p.second);
c = max(c, p.first);
d = max(d, p.second);
}
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}
```

# hihoCoder 1551-合并子目录

hihoCoder 1551-合并子目录

### 描述

/hihocoder/offer22/solutions/p1
/hihocoder/challenge30/p1/test
/game/moba/dota2/uninstall

/hihocoder
/hihocoder/offer22
/hihocoder/offer22/solutions
/hihocoder/challenge30
/hihocoder/challenge30/p1
/game
/game/moba
/game/moba/dota2/

### 输出

```3
/hihocoder/offer22/solutions/p1
/hihocoder/challenge30/p1/test
/game/moba/dota2/uninstall```

`8`

```#include<algorithm>
#include<vector>
#include<iostream>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
struct Node {
string key_;
map<string, Node*> children_;
Node(string key) :key_(key) {};
};
int main() {
//freopen("input.txt", "r", stdin);
int n;
scanf("%d\n", &n);
string line;
Node* root = new Node("");
while (n--) {
getline(cin, line);
int pre = 0;
int pos = line.find('//');
Node* cur = root;
while (pos != string::npos) {
if (pos != 0) {
string tmp = line.substr(pre + 1, pos - pre - 1);
if (cur->children_[tmp] == NULL) {
cur->children_[tmp] = new Node(tmp);
}
cur = cur->children_[tmp];
}
pre = pos;
pos = line.find('//', pos + 1);
}
}
ll ans = 0;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* cur = q.front();
q.pop();
++ans;
for (auto it : cur->children_) {
q.push(it.second);
}
}
printf("%lld\n", ans - 1);
return 0;
}
```

# hihoCoder 1550-顺序三元组

hihoCoder 1550-顺序三元组

### 输出

```6
1 3 2 1 2 3```

`3`

```#include<algorithm>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
int main() {
//freopen("input.txt", "r", stdin);
int n = 0;
scanf("%d", &n);
vector<int> nums(n, 0);
map<int, int> left, right;
for (int i = 0; i < n; ++i) {
scanf("%d", &nums[i]);
++right[nums[i]];
}
ll ans = 0;
for (int i = 0; i < n; ++i) {
--right[nums[i]];
if (nums[i] == 2) {
ans += left[1] * right[3];
}
++left[nums[i]];
}
printf("%lld\n", ans);
return 0;
}
```

# LeetCode Find K Closest Elements

LeetCode Find K Closest Elements
Given a sorted array, two integers `k` and `x`, find the `k` closest elements to `x` in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.
Example 1:

```Input: [1,2,3,4,5], k=4, x=3
Output: [1,2,3,4]
```

Example 2:

```Input: [1,2,3,4,5], k=4, x=-1
Output: [1,2,3,4]
```

Note:

1. The value k is positive and will always be smaller than the length of the sorted array.
2. Length of the given array is positive and will not exceed 104
3. Absolute value of elements in the array and x will not exceed 104

```class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
vector<int> ans;
int n = arr.size();
auto it = lower_bound(arr.begin(), arr.end(), x);
if (it == arr.begin()) {
while (ans.size() < k) {
ans.push_back(*it++);
}
return ans;
}
else if (it == arr.end()) {
while (ans.size() < k) {
ans.push_back(*--it);
}
return ans;
}
else {
int right = it - arr.begin();
int left = right - 1;
int left_diff = INT_MAX, right_diff = INT_MAX;
while (ans.size() < k) {
if (left >= 0)left_diff = abs(arr[left] - x);
else left_diff = INT_MAX;
if (right < n)right_diff = abs(arr[right] - x);
else right_diff = INT_MAX;
if (left_diff <= right_diff) {
ans.push_back(arr[left]);
--left;
}
else {
ans.push_back(arr[right]);
++right;
}
}
}
sort(ans.begin(), ans.end());
return ans;
}
};
```

# LeetCode Judge Route Circle

LeetCode Judge Route Circle
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are `R` (Right), `L` (Left), `U` (Up) and `D` (down). The output should be true or false representing whether the robot makes a circle.
Example 1:

```Input: "UD"
Output: true
```

Example 2:

```Input: "LL"
Output: false```

```class Solution {
public:
bool judgeCircle(string moves) {
int horizon = 0, vertical = 0;
for (auto c : moves) {
if (c == 'U')++vertical;
else if (c == 'D')--vertical;
else if (c == 'L')--horizon;
else if (c == 'R')++horizon;
}
return horizon == 0 && vertical == 0;
}
};
```

# hihoCoder 1543-SCI表示法

hihoCoder 1543-SCI表示法

### 输出

```2
15
8```

```5
1```

```#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>
#include<string>
#include<unordered_map>
#include<queue>
using namespace std;
typedef long long ll;
ll t, n;
int main() {
freopen("input.txt", "r", stdin);
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
if (n == 1 || n == 2) {
printf("1\n");
} else {
ll i = 1, j = 2;
ll sum = i + j, ans = 0;
while (true) {
while (j <= n && sum < n) {
++j;
sum += j;
}
if (j > n)break;
while (i < j && sum > n) {
sum -= i;
++i;
}
if (sum == n) {
//printf("%d\n", j - i + 1);
ans = max(ans, j - i + 1);
break;
}
}
printf("%lld\n", ans);
}
}
return 0;
}
```

```#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>
#include<string>
#include<unordered_map>
#include<queue>
using namespace std;
typedef long long ll;
ll t, n;
int main() {
freopen("input.txt", "r", stdin);
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
if (n == 1 || n == 2) {
printf("1\n");
}
else {
ll ans = 1;
for (ll i = 1; i < n; ++i) {
ll sum = (1 + i)*i / 2;
if (sum > n)break;
ll left = sum - n;
if (left%i == 0) {
ans = max(ans, i);
}
}
printf("%lld\n", ans);
}
}
return 0;
}
```

# hihoCoder 1542-无根数变有根树

hihoCoder 1542-无根数变有根树

### 输出

```5 4
1 2
3 1
4 3
5 1```

`3 1 4 0 1`

```#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>
#include<string>
#include<unordered_map>
#include<queue>
using namespace std;
const int kMaxN = 1005;
int n, k;
vector<int> parent(kMaxN, 0);
vector<vector<int>> graph(kMaxN, vector<int>(kMaxN, 0));
void DFS(int node, int pre) {
parent[node] = pre;
graph[node][pre] = 0;
graph[pre][node] = 0;
for (int i = 1; i <= n; ++i) {
if (graph[node][i] == 1) {
DFS(i, node);
}
}
graph[node][pre] = 1;
graph[pre][node] = 1;
}
int main() {
//freopen("input.txt", "r", stdin);
scanf("%d %d", &n, &k);
int a, b;
for (int i = 0; i < n - 1; ++i) {
scanf("%d %d", &a, &b);
graph[a][b] = 1;
graph[b][a] = 1;
}
DFS(k, 0);
for (int i = 1; i <= n; ++i)
printf("%d ", parent[i]);
return 0;
}
```

# LeetCode Dota2 Senate

LeetCode Dota2 Senate
In the world of Dota2, there are two parties: the `Radiant` and the `Dire`.
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise `one` of the two rights:

1. `Ban one senator's right`:
A senator can make another senator lose all his rights in this and all the following rounds.
2. `Announce the victory`:
If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.

Given a string representing each senator's party belonging. The character 'R' and 'D' represent the `Radiant` party and the `Dire` party respectively. Then if there are `n` senators, the size of the given string will be `n`.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be `Radiant` or `Dire`.
Example 1:

```Input: "RD"
Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1.
And the second senator can't exercise any rights any more since his right has been banned.
And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
```

Example 2:

```Input: "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in the round 1.
And the second senator can't exercise any rights anymore since his right has been banned.
And the third senator comes from Dire and he can ban the first senator's right in the round 1.
And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
```

Note:

1. The length of the given string will in the range [1, 10,000].

1. DXDRR
2. DXDXR
3. XXDXR
4. XXDXX

Dire胜利；第一个D如果杀其后的第二个R：

1. DRDXR
2. DRXXR
3. XRXXR

round-based的方式是用%n的方式实现，代码如下：

```class Solution {
public:
string predictPartyVictory(string senate) {
int r_cnt = 0, d_cnt = 0, n = senate.size();
for (int i = 0; i < n; ++i) {
if (senate[i] == 'R')
++r_cnt;
else
++d_cnt;
}
int pos = 0;
while (r_cnt > 0 && d_cnt > 0) {
if (senate[pos] == 'X') {
pos = (pos + 1) % n;
continue;
}
int pos_next = (pos + 1) % n;
while (senate[pos_next] == senate[pos] || senate[pos_next] == 'X')
pos_next = (pos_next + 1) % n;
if (senate[pos_next] == 'R')
--r_cnt;
else
--d_cnt;
senate[pos_next] = 'X';
pos = (pos + 1) % n;
}
return r_cnt == 0 ? "Dire" : "Radiant";
}
};
```

# LeetCode 4 Keys Keyboard

LeetCode 4 Keys Keyboard
Imagine you have a special keyboard with the following keys:
`Key 1: (A)`: Prints one 'A' on screen.
`Key 2: (Ctrl-A)`: Select the whole screen.
`Key 3: (Ctrl-C)`: Copy selection to buffer.
`Key 4: (Ctrl-V)`: Print buffer on screen appending it after what has already been printed.
Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of 'A' you can print on screen.
Example 1:

```Input: N = 3
Output: 3
Explanation:
We can at most get 3 A's on screen by pressing following key sequence:
A, A, A
```

Example 2:

```Input: N = 7
Output: 9
Explanation:
We can at most get 9 A's on screen by pressing following key sequence:
A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V
```

Note:

1. 1 <= N <= 50
2. Answers will be in the range of 32-bit signed integer.

• A: 打印一个字符A
• Ctrl-A: 全选
• Ctrl-C: 复制
• Ctrl-V: 粘贴

```class Solution {
public:
int maxA(int N) {
if (N <= 6)return N;
vector<int> dp(N + 1, 0);
for (int i = 1; i <= 6; ++i)dp[i] = i;
for (int i = 7; i <= N; ++i) {
for (int b = i - 3; b >= 1; --b) {
dp[i] = max(dp[i], dp[b] + dp[b] * (i - b - 2));
}
}
return dp[N];
}
};
```

# LeetCode 2 Keys Keyboard

LeetCode 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

1. `Copy All`: You can copy all the characters present on the notepad (partial copy is not allowed).
2. `Paste`: You can paste the characters which are copied last time.

Given a number `n`. You have to get exactly `n` 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get `n` 'A'.
Example 1:

```Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.
```

Note:

1. The `n` will be in the range [1, 1000].

```class Solution {
private:
struct Node {
int presented_cnt_; // 现在有多少个字符
int copied_cnt_; // 剪切板中有多少个字符
int step_; // 走过多少步了
Node(int p, int c, int s) :presented_cnt_(p), copied_cnt_(c), step_(s) {};
};
public:
int minSteps(int n) {
queue<Node> q;
Node node(1, 0, 0);
q.push(node);
while (!q.empty()) {
Node cur = q.front();
q.pop();
if (cur.presented_cnt_ == n)
return cur.step_;
if (cur.presented_cnt_ > n)continue;
Node copy_node(cur.presented_cnt_, cur.presented_cnt_, cur.step_ + 1); // 全选
q.push(copy_node);
if (cur.copied_cnt_ != 0) { // 粘贴
Node paste_node(cur.presented_cnt_ + cur.copied_cnt_, cur.copied_cnt_, cur.step_ + 1);
q.push(paste_node);
}
}
}
};
```

```class Solution {
public:
int minSteps(int n) {
vector<int> dp(n + 1, INT_MAX);
dp[1] = 0;
for (int i = 2; i <= n; ++i) {
dp[i] = i;
for (int j = i - 1; j >= 1; --j) {
if (i%j == 0) {
dp[i] = min(dp[i], dp[j] + i / j);
}
}
}
return dp[n];
}
};
```

```class Solution {
public:
int minSteps(int n) {
vector<int> dp(n + 1, INT_MAX);
dp[1] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 2 * i, k = 2; j <= n; j += i, ++k) {
dp[j] = min(dp[j], dp[i] + k);
}
}
return dp[n];
}
};
```