字典树数组形式写法
来源:互联网 发布:苗阜与姜昆的关系知乎 编辑:程序博客网 时间:2024/05/20 01:09
第一题:
Description
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.
Since Jiejie can't remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.
The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.
Input
The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.
The second line contains an integer S , 1S4000 .
Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.
There is a blank line between consecutive test cases.
You should proceed to the end of file.
Output
For each test case, output the number, as described above, from the task description modulo 20071027.
Sample Input
abcd 4 a b cd ab
Sample Output
Case 1: 2
#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define maxn 400000 + 10#define M 300000 + 10#define mod 20071027int tree[maxn][27], val[maxn], top;int d[maxn];char s[M], tmp[105];void init(){ top = 1; memset(tree[0], 0, sizeof tree[0]); memset(d, 0, sizeof d);}void insert(char *s, int v){ int len = strlen(s), u = 0; for(int i = 0; i < len; i++) { int c = s[i] - 'a' ; if(!tree[u][c]) { memset(tree[top], 0, sizeof tree[top]); val[top] = 0; tree[u][c] = top++; } u = tree[u][c]; } val[u] = v;}int len ;int query(char *s, int pos){ int u = 0; int res = 0; for(int i = pos; i < len; i++) { int c = s[i] - 'a' ; u = tree[u][c]; if(!u) return res; if(val[u]) { res = (res + d[i + 1] ) % mod ; } } return res;}int main(){ int kase = 0; while(~scanf("%s", s)) { init(); len = strlen(s); int n; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%s", tmp); insert(tmp, 1); } d[len] = 1; for(int i = len - 1; i >= 0; i--) { d[i] = query(s, i); } printf("Case %d: %d\n", ++kase, d[0]); } return 0;}
ZYB loves Xor I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 734 Accepted Submission(s): 320
We define that lowbit(x)=
Specially,lowbit(0)=0
Because the ans may be too big.You just need to output
The first line has an integer
The second line has
254 0 2 7 052 6 5 4 0
Case #1: 36Case #2: 40
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define MOD 998244353#define M 2000010using namespace std;int top;// top 表示节点个数int tree[M][2]; // 字典树int val[M]; //字符串的权值 当val[i] 大于0时i是单词节点void init(){ top=1; //memset(tree, 0, sizeof tree); memset(tree[0], 0, sizeof tree[0]); memset(val, 0, sizeof val);}void insert(int v){ int u = 0;//根节点 for(int i = 0; i < 30 ; i++) { int t = v & ( 1<< i ); if(t) t = 1; if(!tree[u][t]) { memset(tree[top], 0, sizeof tree[top]); val[top] = 1; tree[u][t] = top++; } else { val[tree[u][t]]++; } u = tree[u][t]; }}int ans;void find(int v){ int u = 0; for(int i = 0; i < 30; i++) { int t = v & (1 << i); if(t) t = 1; if(tree[u][t ^ 1]) ans = ( ans + (val[tree[u][t ^ 1]]) * (1 << i)) % MOD; u = tree[u][t] ; }}int a[M];int main(){ int T, n; scanf("%d", &T); int kase = 0; while(T--) { init(); ans = 0; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d", &a[i]); insert(a[i]); } for(int i=0; i<n; i++) find(a[i]); printf("Case #%d: %d\n", ++kase, ans); } return 0;}
- 字典树数组形式写法
- iOS 数组 字典新写法
- 【Trie】Trie字典树模板 静态指针池、数组写法
- iOS 数组 和字典的新写法
- Block方式遍历字典数组 以及字典数组的新写法 NSNotification的写法
- POJ2418 二叉排序树 && 字典树写法
- POJ2418 二叉排序树 && 字典树写法
- 字典树数组实现
- 《字典树》数组模版
- 数组字典树模板
- 字典的写法
- 字典树两种写法....
- 总结:数字封装与数组,字典的写法和语法糖
- HDU1075字典树初体验和map写法
- 字典数组
- 数组形式二叉树之C++封装
- HDU1075静态数组实现字典树
- HDOJ5687 字典树模板,数组实现
- iOS dSYM文件结构剖析(下)
- vim 缩进设置
- Java实现二维码QRCode的编码和解码
- android常用工具类 --事件
- Android PorterDuff及Xfermode详解
- 字典树数组形式写法
- C#高级编程七十二天----DynamicObject和ExpandoObject
- 二 cocos2dx 之 解析xml文件数据并在程序中调用,存储
- HDU 2157 How many ways?? (DP)
- Java 里把 InputStream 转换成 String 的几种方法
- iPhone6分辨率与适配
- twisted-02 ChatRoom
- Java 并发专题 :闭锁 CountDownLatch
- linux