zoukankan      html  css  js  c++  java
  • 单词拼写检查

    2017-07-28 16:12:51

    writer:pprp

    题目简单描述:

    第一行,N代表单词库中有N个单词,随后是N行单词,紧接着M代表待查单词数目,随后是M行待查单词

    输出:拼写单词错误的单词数目

    算法:用到了哈希表进行快速查找


    代码如下:

    #include <iostream>
    #include <string>
    
    using namespace std;
    const int maxn = 112337;
    int total;
    string h[maxn+1];
    
    int Hash(string x)
    {
        int l = x.length();
        int m = l/2;
        int t = (x[0]-65)*10000+(x[m]-65)*100+x[l-1]-65;   //取值方法
        return t%maxn;
    }
    
    void Insert(string x)
    {
        int t = Hash(x);
        while(h[t]!=""&&h[t]!=x)
        {
            t++;
            if(t==maxn)
                t=0;
        }
        h[t]=x;
    }
    
    void Find(string x)
    {
          int t = Hash(x);
          while(h[t]!=""&&h[t]!=x)
          {
                t++;
                if(t==maxn)
                      t=0;
          }
          if(h[t]=="")
                total++;
    }
    
    
    int main()
    {
        int i,n,m;
        string tmp;
        cin >> n;          //输入到单词库中的单词数目
        for(i=1;i<=n;i++)
        {
              cin >>tmp;
              Insert(tmp);
        }
        cin >> m;          //待查单词数目
        for(i=1;i<=m;i++)
        {
              cin >> tmp;
              Find(tmp);
        }
        cout << total;
        
        return 0;
    }
  • 相关阅读:
    1370
    1336
    1298
    1289
    Buy Tickets(poj2828)
    The Balance(poj2142)
    1067
    Lightoj1011
    1319
    Back to Underworld(lightoj 1009)
  • 原文地址:https://www.cnblogs.com/pprp/p/7251422.html
Copyright © 2011-2022 走看看