#include <stdio.h>#include <string>#include <map>using namespace std;map<string, string> mp, mpp;char c[110][30];char str[110];string s;int main(){int T, n, m, i, j, ri = 1;scanf("%d", &T);while (T--){mp.clear();mpp.clear();scanf("%d%d", &n, &m);for (i = 0; i < m; i++){scanf("%s%s", c[i], str);mp[c[i]] = str;}for (i = 0; i < m; i++){s = c[i];for (j = 1; j < n; j++){if (mp.count(s) == 0) break;s = mp[s];}mpp[c[i]] = s;}gets(str);gets(str);printf("Case #%d: ", ri++);s = "";for (i = 0; str[i]; i++){if (str[i] >= 'a' && str[i] <= 'z') s += str[i];else{printf("%s%c", (mpp.count(s)==0)?s.c_str():mpp[s].c_str(), str[i]);s = "";}}if (s != "") printf("%s", (mpp.count(s)==0)?s.c_str():mpp[s].c_str());printf("n");}return 0;}
二、长方形
时间限制: 1000ms 内存限制: 256MB
描述
在 N 条水平线与 M 条竖直线构成的网格中,放 K 枚石子,每个石子都只能放在网格的交叉点上。问在最优的摆放方式下,最多能找到多少四边平行于坐标轴的长方形,它的四个角上都恰好放着一枚石子。
输入
输入文件包含多组测试数据。
第一行,给出一个整数T,为数据组数。接下来依次给出每组测试数据。
每组数据为三个用空格隔开的整数 N,M,K。
输出
对于每组测试数据,输出一行
"Case #X: Y"
,其中X表示测试数据编号,Y表示最多能找到的符合条件的长方形数量。所有数据按读入顺序从1开始编号。
数据范围
1 ≤ T ≤ 100
0 ≤ K ≤ N * M
小数据:0 < N, M ≤ 30
大数据:0 < N, M ≤ 30000
样例输入
3
3 3 8
4 5 13
7 14 86
样例输出
Case #1: 5
Case #2: 18
Case #3: 1398
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;long long f(long long x){return x * (x - 1) / 2;}int main(){long long ans = 0;int T, n, m, k, ri = 1, x, y, z;scanf("%d", &T);while (T--){scanf("%d%d%d", &n, &m, &k);ans = 0;for (x = 1; x <= n; x++){y = k / x;z = k % x;if (y + (z?1:0) > m) continue;ans = max(f(x) * f(y) + y * f(z), ans);}for (x = 1; x <= m; x++){y = k / x;z = k % x;if (y + (z?1:0) > n) continue;ans = max(f(x) * f(y) + y * f(z), ans);}cout << "Case #" << ri++ << ": " << ans << endl;}return 0;}
三、树上的三角形
时间限制: 2000ms 内存限制: 256MB
描述
有一棵树,树上有只毛毛虫。它在这棵树上生活了很久,对它的构造了如指掌。所以它在树上从来都是走最短路,不会绕路。它还还特别喜欢三角形,所以当它在树上爬来爬去的时候总会在想,如果把刚才爬过的那几根树枝/树干锯下来,能不能从中选三根出来拼成一个三角形呢?
输入
输入数据的第一行包含一个整数 T,表示数据组数。
接下来有 T 组数据,每组数据中:
第一行包含一个整数 N,表示树上节点的个数(从 1 到 N 标号)。
接下来的 N-1 行包含三个整数 a, b, len,表示有一根长度为 len 的树枝/树干在节点 a 和节点 b 之间。
接下来一行包含一个整数 M,表示询问数。
接下来M行每行两个整数 S, T,表示毛毛虫从 S 爬行到了 T,询问这段路程中的树枝/树干是否能拼成三角形。
输出
对于每组数据,先输出一行
"Case #X:"
,其中X为数据组数编号,从 1 开始。
接下来对于每个询问输出一行,包含
"Yes"
或“No”,表示是否可以拼成三角形。
数据范围
1 ≤ T ≤ 5
小数据:1 ≤ N ≤ 100, 1 ≤ M ≤ 100, 1 ≤ len ≤ 10000
大数据:1 ≤ N ≤ 100000, 1 ≤ M ≤ 100000, 1 ≤ len ≤ 1000000000
样例输入
2
5
1 2 5
1 3 20
2 4 30
4 5 15
2
3 4
3 5
5
1 4 32
2 3 100
3 5 45
4 5 60
2
1 4
1 3
样例输出
Case #1:
No
Yes
Case #2:
No
Yes
#include <stdio.h>#include <vector>#include <algorithm>using namespace std;int deep[100010], f[100010][2];vector<int> e[100010];int c[60], cn;void dfs(int fa, int x, int d){int i, y;deep[x] = d;for (i = 0; i < (int)e[x].size(); i += 2){y = e[x][i];if (y == fa) continue;f[y][0] = x;f[y][1] = e[x][i + 1];dfs(x, y, d + 1);}}void pd(int x, int y){while (deep[x] > deep[y]){c[cn++] = f[x][1];x = f[x][0];if (cn == 50) return;}while (deep[y] > deep[x]){c[cn++] = f[y][1];y = f[y][0];if (cn == 50) return;}while (x != y){c[cn++] = f[x][1];x = f[x][0];c[cn++] = f[y][1];y = f[y][0];if (cn >= 50) return;}}int main(){int T, ri = 1, n, m, x, y, z, i;scanf("%d", &T);while (T--){scanf("%d", &n);for (i = 1; i <= n; i++) e[i].clear();for (i = 1; i < n; i++){scanf("%d%d%d", &x, &y, &z);e[x].push_back(y);e[x].push_back(z);e[y].push_back(x);e[y].push_back(z);}dfs(0, 1, 0);printf("Case #%d:n", ri++);scanf("%d", &m);while (m--){scanf("%d%d", &x, &y);cn = 0;pd(x, y);sort(c, c + cn);for (i = 0; i + 2 < cn; i++){if (c[i] + c[i + 1] > c[i + 2]) break;}if (i + 2 < cn) printf("Yesn");else printf("Non");}}return 0;}