Trie 集合

来源:互联网 发布:内控风险控制矩阵 编辑:程序博客网 时间:2024/05/19 23:03

POJ 2418

题意:给你一堆字符串,然后按字典序输出,再输出他们出现的频率。

思路:trie。

#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <iostream>#include <algorithm>#define Max 2505#define FI first#define SE second#define ll long long#define PI acos(-1.0)#define inf 0x3fffffff#define LL(x) ( x << 1 )#define bug puts("here")#define PII pair<int,int>#define RR(x) ( x << 1 | 1 )#define mp(a,b) make_pair(a,b)#define mem(a,b) memset(a,b,sizeof(a))#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )using namespace std;/*********************************************/#define N 111111struct TT{    int next[128] ;    int count ;    void init(){        mem(next ,0) ;        count = 0 ;    }}T[N] ;int num = 0 ;void insert(char *a){    int l = strlen(a) ;    int now = 0 ;    for (int i = 0 ; i < l ; i ++ ){        int x = a[i] ;        if(T[now].next[x] == 0){            T[now].next[x] = ++ num ;            T[num].init() ;        }        now = T[now].next[x] ;    }    T[now].count ++ ;}char ans[11111] ;int cnt = 0 ;void dfs(int now ,int dp){//    int fkk = 0 ;    if(T[now].count){        ans[dp] = '\0' ;        cout << ans <<" " ;        printf("%.4f\n",T[now].count * 1.0 / cnt * 100) ;    }    for (int i = 0 ; i < 128 ; i ++ ){        if(T[now].next[i]){//            fkk ++ ;            ans[dp] = char(i) ;            dfs(T[now].next[i] , dp + 1 ) ;        }    }}char sfk[111] ;int main() {    num = 0 ;    cnt = 0 ;    T[0].init() ;    while(gets(sfk)){        int l = strlen(sfk) ;        if(!l)break ;        cnt ++ ;        insert(sfk) ;    }    dfs(0 , 0) ;    return 0 ;}


POJ 1056 还是水题,就不开新的一篇博客了。

#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <iostream>#include <algorithm>#define Max 2505#define FI first#define SE second#define ll long long#define PI acos(-1.0)#define inf 0x3fffffff#define LL(x) ( x << 1 )#define bug puts("here")#define PII pair<int,int>#define RR(x) ( x << 1 | 1 )#define mp(a,b) make_pair(a,b)#define mem(a,b) memset(a,b,sizeof(a))#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )using namespace std;#define N 111111struct TT{    int next[2] ;    int count ;    void init(){        mem(next ,0) ;        count = 0 ;    }}T[N] ;int num = 0 ;bool flag = 0 ;void insert(char *a){    int l = strlen(a) ;    int now = 0 ;    for (int i = 0 ; i < l ; i ++ ){        int x = a[i] - '0' ;        if(T[now].count)flag = 1 ;        if(T[now].next[x] == 0){            T[now].next[x] = ++ num ;            T[num].init() ;        }        now = T[now].next[x] ;    }    T[now].count ++ ;}void init(){    T[0].init() ;num = 0 ;}char in[N] ;int main() {    int ca = 0 ;    while(scanf("%s",in) != EOF){        if(in[0] != '9'){            insert(in) ;        }        else {            if(flag)            printf("Set %d is not immediately decodable\n",++ca) ;            else            printf("Set %d is immediately decodable\n",++ca) ;            flag = 0 ;            init() ;        }    }    return 0 ;}



POJ 1204

这里

POJ 2513

这里

ZOJ 3228

这里

HDU 1671 POJ 3630

这里

HDU 2846

这里

POJ 2001

这里

POJ 1035 这题我直接枚举的,貌似可以用TRIE写。

#define N 1111111char in[N][22] ;char out[N] ;bool vis[11111] ;int insertanddelete(char *a , char *b){    int la = strlen(a) ;    int lb = strlen(b) ;    if(la <= lb)return 0 ;    int nn = 0 ;    int bb = 0 ;    for (int i = 0 ; i < la ; i ++ ){        if(a[i] == b[bb]){            nn ++ ;            bb ++ ;        }        else {            continue ;        }    }    if(nn == lb)return 1 ;return 0 ;}int replace(char *a ,char *b){    int la = strlen(a) ;    int lb = strlen(b) ;    if(la != lb)return 0 ;    int nn = 0 ;    for (int i = 0 ; i < la ; i ++ ){        if(a[i] != b[i])nn ++ ;    }    if(nn == 1)return 1 ;return 0 ;}int main(){    int num = 1 ;    while(scanf("%s",in[num]) != EOF){        if(in[num][0] != '#'){            num ++ ;        }        else {            while(scanf("%s",out) , out[0] != '#'){                bool flag = 0 ;                mem(vis ,0) ;                int lb = strlen(out) ;                for (int i = 1 ; i < num ; i ++ ){                    int la = strlen(in[i]) ;                    if(la == lb){                        int xx = 0 ;                        for (int j = 0 ; j < la ; j ++ ){                            if(in[i][j] != out[j]) xx ++ ;                        }                        if(xx == 0)flag = 1 ;                        if(flag)break ;                    }                    if(abs(la - lb) > 1)continue ;                    if(insertanddelete(in[i] , out) || insertanddelete(out , in[i]) || replace(in[i] , out)){                        vis[i] = 1 ;                    }                }                if(flag){                    printf("%s is correct\n",out) ;                }                else {                    printf("%s:",out) ;                    for (int i = 1 ; i < num ; i ++ )if(vis[i])printf(" %s",in[i]) ;                    puts("") ;                }            }        }    }    return 0 ;}