zoukankan      html  css  js  c++  java
  • 1095 Anigram单词(51nod)

    原题链接:http://www.51nod.com/onlineJudge/questionCode.html#problemId=1095&noticeId=20791

    老实说这题,我是很不想用字典树,因为不熟~~~但后来没搞出来,就用了字典树。。。。。。。。。。当然得参考一下大神的代码。

    解法:用map统计相同单词。。。

    然后把字符串按字典序排序,然后把字符串插入到字典树里面去

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<map> 
    #include<vector> 
    #define maxn 10000 
    using namespace std;
    typedef struct node
    {
        int count;
        struct node *next[100];//因为有大写字母,所以数组要开的大点,防止越界
    }*tree;
    void insert(tree h,string s)
    {
        int len=s.length();
        tree p=h;
        for(int i=0;i<len;i++)
        {
            int index=s[i]-'A';
            if(p->next[index]!=NULL)
            {
                p=p->next[index];
                p->count++;
            }
            else
            {
               tree tem=(tree)calloc(1,sizeof(node));
                tem->count=1;
                p->next[index]=tem;
                p=tem;
            }
        }
    }
    int find(tree h,string s)
    {
        tree p=h;
        int len=s.length(); 
        for(int i=0;i<len;i++) 
        {
            int index=s[i]-'A';
            if(p->next[index]==NULL)
            return -1;
            p=p->next[index];
        }
        int ans=p->count;
        for(int i=0;i<100;i++)
        {
           if(p->next[i]!=NULL)
           ans-=p->next[i]->count;//其他分支要减掉
        }
        return ans;
    }
    int main()
    {
        int n,q;
        string s;
        tree h=(tree)calloc(1,sizeof(node));
        scanf("%d %d",&n);
        map<string,int>S;
        for(int i=0;i<n;i++)
        {
            cin>>s;
            S[s]++; 
            sort(s.begin(),s.end());
            insert(h,s);
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int ans=0;
            string tmp;
            cin>>tmp;
            ans-=S[tmp];
            sort(tmp.begin(),tmp.end());
            int x=find(h,tmp);
            if(x==-1)
            printf("0
    ");
            else
            printf("%d
    ",ans+x);
        }
        return 0;
    }
  • 相关阅读:
    Taobao File System
    poj 1860 (Bellman_Ford判断正环)
    IOS研究院之打开照相机与本地相册选择图片
    mysql中timestamp,datetime,int类型的区别与优劣
    数学之路(3)-机器学习(3)-机器学习算法-贝叶斯定理(3)
    POJ 3740 DLX
    HDU4738【杭州网赛、判桥】
    umdh工具使用
    计算机组成与设计(八)—— 单周期处理器
    计算机组成与设计(七)—— 除法器
  • 原文地址:https://www.cnblogs.com/NaCl/p/4758245.html
Copyright © 2011-2022 走看看