Ural 1542. Autocompletion(二分)
来源:互联网 发布:mac和windows怎么切换 编辑:程序博客网 时间:2024/05/03 01:59
题目链接:点击打开链接
思路:
因为单词最长15, 我们把每个单词不同长度的前缀存起来, 排序之后二分即可, 复杂度O(nlogn)
细节参见代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <ctime>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 1e5 + 10;int T,n,m,L[maxn];struct node { char s[16], name[16]; int p; node(char _s[], char str[], int _p) { strcpy(s, _s); strcpy(name, str); p = _p; } node() {} bool operator < (const node& rhs) const { int cur = strcmp(name, rhs.name); if(cur == 0) { if(p == rhs.p) { int cmp = strcmp(s, rhs.s); return cmp < 0; } else return p > rhs.p; } else return cur < 0; }};node a[maxn];char op[19];vector<node> b[17];int main() { while(~scanf("%d", &n)) { int maxlen = 0; for(int i = 1; i <= n; i++) { scanf("%s%d", a[i].s, &a[i].p); L[i] = strlen(a[i].s); maxlen = max(maxlen, L[i]); } for(int i = 1; i <= maxlen; i++) b[i].clear(); for(int i = 1; i <= n; i++) { for(int j = 0; j < L[i]; j++) { op[j] = a[i].s[j]; op[j+1] = 0; b[j+1].push_back(node(a[i].s, op, a[i].p)); } } for(int i = 1; i <= maxlen; i++) sort(b[i].begin(), b[i].end()); scanf("%d", &m); while(m--) { scanf("%s", op); int len = strlen(op); int pos = lower_bound(b[len].begin(), b[len].end(), node(op, op, INF)) - b[len].begin(); int lenth = b[len].size(); for(int i = pos; i < lenth; i++) { if(strcmp(b[len][i].name, op) != 0) break; printf("%s\n", b[len][i].s); if(i - pos + 1 >= 10) break; } if(m) printf("\n"); } } return 0;}
0 0
- Ural 1542. Autocompletion(二分)
- URAL 1542. Autocompletion (线段树+STL)
- URAL 1542. Autocompletion 字典树
- URAL-1542 Autocompletion (字典序)
- URAL 1542: Autocompletion
- Ural 1998 The old Padawan(二分)
- URAL 1196. History Exam (二分)
- URAL 2034 Caravans (最短路 + 二分)
- URAL 1106. Two Teams (二分图)
- ural 1613【hash+二分】
- URAL - 1066 Garland(二分)
- URAL 1133 二分
- ural 1080. Map Coloring(二分染色bfs)
- Ural 1076 Trash (二分图最佳完美匹配)
- URAL 1980 Road to Investor(二分+最短路)
- URAL 1209. 1, 10, 100, 1000... (规律 + 二分)
- URAL 2038 Minimum Vertex Cover(二分匹配)
- URAL 1153. Supercomputer 二分求根
- 试图更改利润中心,但物料库存仍然存在
- On distributed Gibbs sampling with Spark
- 【Android】04、控件的获取方法
- 多种方法对iOS数组中的元素去重复
- ps命令使用 进程查看
- Ural 1542. Autocompletion(二分)
- 服务器常用端口号
- 横竖屏切换监听 android
- CURL接口请求
- Incompatible JavaHL library loaded. 1.9.x or later required
- 浮动 float
- MQTT服务器搭建-mosquitto1.4.4安装指南
- Android:销毁所有的Activity退出应用程序几种方式
- JAVA-泛型的简单使用方法以及高级使用