IOS 版本的Acwing 什么时候能在App store里面上线啊?
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
class Solution {
public:
bool hasPath(vector<vector<char>>& matrix, string &str) {
for (int i = 0; i < matrix.size(); i++)
for (int j = 0; j < matrix[i].size(); j++)
if (dfs(matrix, str, 0, i, j))
return true;
return false;
}
bool dfs(vector<vector<char>> &matrix, string &str, int u, int x, int y) {
if (matrix[x][y] != str[u]) return false;
if (u == str.size() - 1) return true;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
char t = matrix[x][y];
matrix[x][y] = '*';
for (int i = 0; i < 4; i ++ ) {
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < matrix.size() && b >= 0 && b < matrix[a].size()) {
if (dfs(matrix, str, u + 1, a, b)) return true;
}
}
matrix[x][y] = t;
return false;
}
};
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
class Solution {
public:
int findMin(vector<int>& nums) {
int n = nums.size() - 1;
if (n < 0) return -1;
while (n > 0 && nums[n] == nums[0]) n -- ;
if (nums[n] >= nums[0]) return nums[0];
int l = 0, r = n;
while (l < r) {
int mid = l + r >> 1;
if (nums[mid] < nums[0]) r = mid;
else l = mid + 1;
}
return nums[r];
}
};
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
string str;
cin >> str;
int len = str.size() / 2;
int left = stoi(str.substr(0, len));
int right = stoi(str.substr(len));
int num = stoi(str);
if (left * right && num % (left * right) == 0) puts("Yes");
else puts("No");
}
return 0;
}
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510;
int n, m, S, T;
int w[N];
int g[N][N];
int dist[N];
int cnt[N], sum[N];
bool st[N];
int dijkstra() {
memset(dist, 0x3f, sizeof dist);
dist[S] = 0, cnt[S] = 1, sum[S] = w[S];
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 0; j < n; j ++)
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
for (int j = 0; j < n; j ++)
if (dist[j] > dist[t] + g[t][j]) {
dist[j] = dist[t] + g[t][j];
cnt[j] = cnt[t]; //最短路条数=上个点的条数
sum[j] = sum[t] + w[j];
}
else if (dist[j] == dist[t] + g[t][j]) {
cnt[j] += cnt[t]; //最短路条数+上个点的条数
sum[j] = max(sum[j], sum[t] + w[j]);
}
}
}
int main() {
cin >> n >> m >> S >> T;
for (int i = 0; i < n; i++) cin >> w[i];
memset(g, 0x3f, sizeof g);
while (m--) {
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
}
dijkstra();
cout << cnt[T] << ' ' << sum[T] << endl;
return 0;
}
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1050010;
// 稀疏图用邻接表来存
int h[N], e[N], ne[N], idx;
int w[N]; // 用来存权重
int dist[N];
bool st[N]; // 如果为true说明这个点的最短路径已经确定
int n, m;
void add(int x, int y, int c) {
// 有重边也不要紧,假设1->2有权重为2和3的边,再遍历到点1的时候2号点的距离会更新两次放入堆中
// 这样堆中会有很多冗余的点,但是在弹出的时候还是会弹出最小值2+x(x为之前确定的最短路径),
// 并标记st为true,所以下一次弹出3+x会continue不会向下执行。
w[idx] = c;
e[idx] = y;
ne[idx] = h[x];
h[x] = idx++;
}
int dijkstra() {
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap; // 定义一个小根堆
// 这里heap中为什么要存pair呢,首先小根堆是根据距离来排的,所以有一个变量要是距离,
// 其次在从堆中拿出来的时候要知道知道这个点是哪个点,不然怎么更新邻接点呢?所以第二个变量要存点。
heap.push({ 0, 1 }); // 这个顺序不能倒,pair排序时是先根据first,再根据second,这里显然要根据距离排序
while(heap.size()) {
PII k = heap.top(); // 取不在集合S中距离最短的点
heap.pop();
int ver = k.second, distance = k.first;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i]; // i只是个下标,e中在存的是i这个下标对应的点。
if(dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
heap.push({ dist[j], j });
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main() {
memset(h, -1, sizeof(h));
scanf("%d%d", &n, &m);
while (m--) {
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
add(x, y, c);
}
cout << dijkstra() << endl;
return 0;
}
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int a[N], b[N];
bool check() {
for (int i = 0; i < n; i ++)
if (a[i] != b[i])
return false;
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < n; i ++) cin >> b[i];
int k = 0;
while (b[k + 1] >= b[k]) k ++ ;
bool match = true;
for (int i = k + 1; i < n; i ++)
if (a[i] != b[i]) {
match = false;
break;
}
if (match) {
puts("Insertion Sort");
sort(b, b + k + 2);
cout << b[0];
for (int i = 1; i < n; i ++) cout << ' ' << b[i];
cout << endl;
}
else {
puts("Merge Sort");
int k = 1;
while (true) {
bool match = check();
int len = 1 << k;
for (int i = 0; i < n; i += len)
sort(a + i, a + min(n, i + len));
if (match) break;
k ++ ;
}
cout << a[0];
for (int i = 1; i < n; i ++) cout << ' '<< a[i];
cout << endl;
}
return 0;
}
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
using namespace std;
const int N = 110;
int n;
int a[N], b[N];
void down(int u, int size) {
int t = u;
if (u * 2 <= size && b[t] < b[u * 2]) t = u * 2;
if (u * 2 + 1 <= size && b[t] < b[u * 2 + 1]) t = u * 2 + 1;
if (t != u) {
swap(b[t], b[u]);
down(t, size);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ ) cin >> b[i];
int p = 2;
while (p <= n && b[p] >= b[p - 1]) p ++ ;
int k = p;
while (p <= n && a[p] == b[p]) p ++ ;
if (p == n + 1) {
puts("Insertion Sort");
while (k > 1 && b[k] < b[k - 1]) swap(b[k], b[k - 1]), k -- ;
}
else {
puts("Heap Sort");
p = n;
while (b[1] <= b[p]) p -- ;
swap(b[1], b[p]);
down(1, p - 1);
}
cout << b[1];
for (int i = 2; i <= n; i ++ ) cout << ' ' << b[i];
cout << endl;
return 0;
}
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
struct Node {
string address;
int key;
string next;
bool operator< (const Node& t) const {
return key < t.key;
}
};
int main() {
int n;
char head[10];
scanf("%d%s", &n, head);
unordered_map<string, Node> map;
char address[10], next[10];
while (n -- ) {
int key;
scanf("%s%d%s", address, &key, next);
map[address] = {address, key, next};
}
vector<Node> nodes;
for (string i = head; i != "-1"; i = map[i].next) nodes.push_back(map[i]);
printf("%d ", nodes.size());
if (nodes.empty()) puts("-1");
else {
sort(nodes.begin(), nodes.end());
printf("%s\n", nodes[0].address.c_str());
for (int i = 0; i < nodes.size(); i ++ ) {
if (i + 1 == nodes.size())
printf("%s %d -1\n", nodes[i].address.c_str(), nodes[i].key);
else
printf("%s %d %s\n", nodes[i].address.c_str(), nodes[i].key, nodes[i + 1].address.c_str());
}
}
return 0;
}