1. 程式人生 > >poj--3630+字典樹基礎題

poj--3630+字典樹基礎題

先按長度排序,先先插入長的,在插入的時候同時進行查詢。

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef struct
{
    char str[12];
}P;
P p[200000];

bool cmp(P p1,P p2)
{
    if(strlen(p1.str)>strlen(p2.str))
        return true;
    return false;
}

typedef struct
{
    int cnt;
    int next[11];
}N;
N node[200000];
int top;

int insert(char *str)
{
   int len=strlen(str);
   int t=0;
   for(int i=0;i<len;i++)
   {
       if(node[t].next[str[i]-'0']==0)
           node[t].next[str[i]-'0']=++top;
       t=node[t].next[str[i]-'0'];
       if(i==len-1&&node[t].cnt==1)
        return 0;
       node[t].cnt=1;
   }
   return 1;
}

char str[1100][12];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(node,0,sizeof(node));
        top=0;
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
           scanf("%s",&p[i].str);
        int i;
        sort(p,p+n,cmp);
        for(i=0;i<n;i++)
        {
            if(insert(p[i].str)==0)
            {
                printf("NO\n");
                break;
            }
        }
        if(i>=n)
            printf("YES\n");
    }
  return 0;
}
</span>

相關推薦

poj--3630+字典基礎

先按長度排序,先先插入長的,在插入的時候同時進行查詢。<span style="font-size:18px;">#include<iostream> #include<

poj 1056 字典

剛剛做出來的 昨晚看的模板,第一次看懂了什麼是字典樹。。汗了,從一開始的結構體定義都不知道。。到現在A過了一題,嘿嘿,挺高興的 寫一下我的理解吧。 主要就是建立一堆一樣的結構體,以ROOT為原點,*NEXT【10】為分支聯絡員,連線著OTHER們,如果輸入的S字串中的

poj--2503+字典入門

字典樹入門題 將字串結尾的標記為一個字串,然後就可以用字典樹的插入和查找了 程式碼如下: #include<iostream> #include<cstring> #inc

poj 3764 字典

plus sample class bsp edge include source 處理 ostream The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total

poj-2001(字典)

turn ios 找不到 clas while 開始 字符串 我們 main 題意:給你一堆字符串,我們定義一個字符串可以被縮寫成一個字符串(必須是原字符串的前綴),問你每個字符串能辨識的前綴是什麽,不能辨識意思是(ab,abc我們縮寫成ab); 解題思路:可以用字典樹解決

CH 1601 - 字首統計 - [字典模板]

題目連結:傳送門 描述給定 $N$ 個字串 $S_1,S_2,\cdots,S_N$,接下來進行 $M$ 次詢問,每次詢問給定一個字串 $T$,求 $S_1 \sim S_N$ 中有多少個字串是 $T$ 的字首。輸入字串的總長度不超過 $10^6$,僅包含小寫字母。 輸入格式第一行兩個整數 $N,M$。接

poj 2104 主席板子

講真這道題我感覺可以暴力 本篇部落格的意思是讓我們理解一下主席樹板子的具體實現細節 給予那些看懂了思路但是看不懂板子的人的一條小道 。。。其實就是主席樹亂講。。。。 表打我這個蒟蒻 >   < #include<iostream> #i

HDU 1247 - Hat’s Words - [字典]

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the dictionary that is the concatenation of exactly two o

字典模板 Shortest Prefixes

B - Shortest Prefixes Time Limit:1000MS     Memory Limit:30000KB     Description A prefix of a string is

字典模板(統計難題 HDU - 1251)

https://vjudge.net/problem/HDU-1251 標準的字典樹模板題: 也注意一下輸入方法: #include<iostream> #include<cstdio> #include<cstring> using namespace std

字典模板 Shortest Prefixes

B - Shortest Prefixes Time Limit:1000MS     Memory Limit:30000KB     Description A prefix of a string is a substring starting at the be

HDU1075 字典板子

題意 :給出兩組字串 一一對映,給出一種組成的文字,要求對映成另外一種思路:使用字典樹,把對映的另外一個字元存在字典樹的單詞節點處  例如 abc   123 則把123存在abc節點中的c處即可 同時這裡使用的是靜態的陣列,操作和寫起來都更方便,就是要提前判斷開的空間,過大

poj 3764 字典求異或最大值

The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4290 Accepted: 952 Description In an edge-weighted

POJ 1451 字典

這題的意思有點類似於一個輸入法,就是按了一些鍵,蹦出單詞庫中頻率最高的那個詞彙 首先給出的是單詞庫,每個單詞有一個出現頻率。 然後給出的是一些詢問,每個詢問有一個字串,代表在手機上按了哪些鍵,最後以1結束。問進行這些按鍵的過程中出現的單詞分別是哪些。 思路就是字典樹了。 以

HDU4825 01字典模板

題解:    第一次知道字典樹還能這樣用,果然還是做題太少了。。ORZ,感覺很多異或的題都可以用字典樹去解決#include<stdio.h> #include<string.h> #include<algorithm> using nam

POJ 2104 劃分模板

給出n,m n個數字 m次詢問,每次詢問(l,r)區間的第k小的數 劃分樹模板 mark一下 #include "stdio.h" #include "string.h" #include "algorithm" using namespace std; int a[

poj 百練 2797(基礎

求簡單,直接用暴力求解方法 對於字典樹不夠熟悉,題目意思有點奇特 #include <fstream> #include <string.h> #include <stdio.h> #include <algorithm>

字典基礎

左右 using names code void 子節點 namespace name 查詢 字典樹:字典樹用來儲存區域信息 主要有五種操作: 建樹、單點查詢、單點修改、區間查詢、區間修改。 #include<bits/stdc++.h> using name

Poj-3630字典,水

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue lis

POJ 3764 The xor-longest Path ( 字典求異或最值 && 異或自反性質 && 好好思想)

strong span -s node poj printf return blog pre 題意 : 給出一顆無向邊構成是樹,每一條邊都有一個邊權,叫你選出一條路,使得此路所有的邊的異或值最大。 分析 : 暴力是不可能暴力的,這輩子不可能暴力,那麽來冷靜分析一下如何去