zoukankan      html  css  js  c++  java
  • hdu2846 Repository

    //---------------------------------------------------------------
    /*---字典树应用问题。考虑到要查询的次数在10^6,显然直接插入后dfs来查找必然超时间。好在每一个单词长度
    ---不超过20,这样可以枚举每个单词子串,然后插入即可。例如abc子串为a,b,c,ab,bc,abc。但是要注意的是同一个
    ---串可能有相同的子串,避免重复插入,可以采用一个节点信息num表示当前插入的是第num个串的子串,可以避免重复插入
    */
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    typedef long long LL;
    using namespace std;
    const int maxsize = 26;
    
    struct Trie{
    	Trie(){ memset(next, 0, sizeof(next)); v = 0; num = -1; }
    	int v;
    	int num;  //标记当前插入的是第num个单词的子串
    	Trie*next[maxsize];
    };
    
    Trie*T;
    void insert(int num,char*str){
    	Trie*p = T;
    	while (*str != ''){
    		int id = *str++ - 'a';
    		if (!p->next[id]) p->next[id] = new Trie;
    		p = p->next[id];
    	}
    	if (num != p->num){
    		p->num=num;
    		p->v++;
    	}
    }
    
    int search(char*str){
    	Trie*p=T;
    	while (*str != ''){
    		int id = *str++ - 'a';
    		if (!p->next[id])return 0;
    		p = p->next[id];
    	}
    	return p->v;
    }
    
    char str[maxsize];
    char temp[maxsize];
    
    int main(){
    	int n, m;
    	T = new Trie;
    	scanf("%d", &n);
    	while (n--){ 
    		scanf("%s", str);
    		int len = strlen(str);
    		for (int l = 1; l <= len;l++)
    		for (int i = 0; i+l<=len;i++){
    			memcpy(temp, str+i, l);
    			temp[l] = '';
    			insert(n, temp);
    		}
    	}
    	scanf("%d", &m);
    	while (m--){
    		scanf("%s", str);
    		printf("%d
    ",search(str));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    特征值分解与奇异值分解的相关学习记录
    week9:Recommender Systems
    关于PCA的一些学习汇总
    第四周疑难点
    第二章感知机习题
    Week7:SVM难点记录
    week6:Diagnosing Bias vs. Variance难点记录
    laravel ajax表格删除
    dropzone
    laravel 部分路由取消csrf
  • 原文地址:https://www.cnblogs.com/td15980891505/p/5885364.html
Copyright © 2011-2022 走看看