zoukankan      html  css  js  c++  java
  • HDOJ1251(前缀匹配---分块查找&map应用)

    分块查找算法

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int SIZE=1300000+16;
    const int BLOCKS=50000; //块的大小
    char word[SIZE][11];
    char pre[20];
    int Num[27];
    bool isPre(char mo[],char so[])
    {
        int i=0,j=0;
        while(mo[i]&&so[j])
        {
            if(mo[i]!=so[j])
                return false;
            i++;
            j++;
        }
        if(so[j]=='')
            return true;
        return false;
    }
    int main()
    {
        int cnt=0;
        char fin[11]={''};
        while(gets(fin))
        {
            if(fin[0]=='')
                break;
            int key=fin[0]-'a';
            int pos=key*BLOCKS+Num[key];
            Num[key]++;
            strcpy(word[pos],fin);
        }
        
        while(scanf("%s",pre)!=EOF)
        {
            int num=0;
            
            int key=(pre[0]-'a');
            int start=key*BLOCKS;
            
            for(int i=start;i<start+Num[key];i++)
            {
                if(isPre(word[i],pre))
                    num++;
            }
            
            printf("%d
    ",num);
        }
        
        return 0;
    }

    利用STL库中的map解决

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<map>
    using namespace std;
    map<string, int> word;
    int main()
    {
        string x="";
        while(true)
        {
            char a;
            scanf("%c",&a);
            if(a=='
    ')
            {
                scanf("%c",&a);
                x="";
            }
            if(a=='
    ')
                break;
            x+=a;
            word[x]++;
        }
        
        string pre;
        while(cin>>pre)
        {
            cout<<word[pre]<<endl;
        }
        //感受到STL的威力了么
        return 0;
    }
  • 相关阅读:
    jQuery中的事件
    Ajax跨域
    javascript的时间委托
    大型数据库优化技巧
    mysql数据库忘记密码时如何修改
    DAY69-nosql
    DAY68-redis
    DAY67-Memcached
    DAY65-apache的安装
    DAY63-centos介绍
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4702509.html
Copyright © 2011-2022 走看看