您的位置:首页 > 其它

UVA - 11732 依旧是字典树板子,和左儿子右兄弟法字典树

2016-08-25 17:50 459 查看

 

依旧是贴上板子,LRJ板

struct Trie//普通字典树板子
{
int ch[maxnode][sigma_size];
int val[maxnode], isnull[maxnode];
int sz;
Trie()
{
sz=1;
memset(ch[0], 0, sizeof(ch[0]));
memset(val, 0, sizeof(val));
memset(isnull, 0, sizeof(isnull));
}
void init()
{
sz=1;
memset(ch[0], 0, sizeof(ch[0]));
memset(val, 0, sizeof(val));
memset(isnull, 0, sizeof(isnull));
}

int idx(char c) {
if (c == '\0') return 62;
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return c - 'A' + 36;
}

void insert(char s[], int len)
{
int u = 0;
for (int i = 0; i <= len; ++ i)
{
int c = idx(s[i]);
if (!ch[u][c])
{
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
val[u] ++;
}
isnull[u]++;
}
}zidian;


ac code

/*
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <tr1/unordered_map>
#include <ctime>
using std::tr1::unordered_map;
*/

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;

/*
using std::sort;
using std::bitset;
using std::max;
using std::cout;
using std::stack;
using std::cin;
using std::endl;
using std::swap;
using std::pair;
using std::vector;
using std::set;
using std::map;
using std::multiset;
using std::queue;
using std::greater;
using std::string;
using std::priority_queue;
using std::max_element;
using std::min_element;

using __gnu_pbds::pairing_heap_tag;
__gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> heap;
#define Hash unordered_map
*/
#define pr(x) cout<<#x<<" = "<<x<<" "
#define prln(x)    cout<<#x<<" = "<<x<<endl

#define lson o*2, L, M
#define rson o*2+1, M + 1,R

typedef long long LL;

//const int maxn = 210000;
const int maxn = 400000+100;
const int maxnode = 4000 * 1000 + 10;
const int sigma_size = 63;

struct Trie
{
int ch[maxnode][sigma_size];
int val[maxnode], isnull[maxnode];
int sz;
Trie()
{
sz=1;
memset(ch[0], 0, sizeof(ch[0]));
memset(val, 0, sizeof(val));
memset(isnull, 0, sizeof(isnull));
}
void init()
{
sz=1;
memset(ch[0], 0, sizeof(ch[0]));
memset(val, 0, sizeof(val));
memset(isnull, 0, sizeof(isnull));
}

int idx(char c) {
if (c == '\0') return 62;
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return c - 'A' + 36;
}

void insert(char s[], int len)
{
int u = 0;
for (int i = 0; i <= len; ++ i)
{
int c = idx(s[i]);
if (!ch[u][c])
{
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
val[u] ++;
}
isnull[u]++;
}
}zidian;

char tmp[2340];
int n;

void init()
{
zidian.init();
for (int i = 1; i <= n;++ i)
{
scanf("%s", tmp);
int len = strlen(tmp);
zidian.insert(tmp, len);
//cout<<tmp<<endl;
}
long long ans = 0;
for (int i = 0; i != zidian.sz; ++ i)
{
long long tmp=0;

for (int j = 0; j < sigma_size; ++ j)
{
if (!zidian.val[zidian.ch[i][j]])	continue;
tmp += zidian.val[zidian.ch[i][j]];
for (int k = j + 1; k < sigma_size; ++ k)
ans +=  zidian.val[zidian.ch[i][j]] * zidian.val[zidian.ch[i][k]];
}
ans += zidian.val[i] * (zidian.val[i] - 1);
}
printf("%d\n", ans);
}

int main()
{
int sb=0;
while (~scanf("%d", &n))
{
if (n==0)	break;
printf("Case %d: ",++sb);
//cout<<endl<<endl;
init();
}
return 0;
}


struct Trie {
int head[maxnode]; // head[i]为第i个结点的左儿子编号
int next[maxnode]; // next[i]为第i个结点的右兄弟编号
char ch[maxnode]; // ch[i]为第i个结点上的字符
int tot[maxnode]; // tot[i]为第i个结点为根的子树包含的叶结点总数
int sz; // 结点总数
long long ans; // 答案
void clear() { sz = 1; tot[0] = head[0] = next[0] = 0; } // 初始时只有一个根结点

// 插入字符串s(包括最后的'\0'),沿途更新tot
void insert(const char *s) {
int u = 0, v, n = strlen(s);
tot[0]++;
for(int i = 0; i <= n; i++) {
// 找字符a[i]
bool found = false;
for(v = head[u]; v != 0; v = next[v])
if(ch[v] == s[i]) { // 找到了
found = true;
break;
}
if(!found) {
v = sz++; // 新建结点
tot[v] = 0;
ch[v] = s[i];
next[v] = head[u];
head[u] = v; // 插入到链表的首部
head[v] = 0;
}
u = v;
tot[u]++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: