map<string,vector<string>>两种字典排序 ——数据结构与算法分析上的

/*************************************************************************
	> File Name: word.cpp
	> Author:keson 
	> Mail:keson@bupt.edu.cn 
	> Created Time: 2014年11月25日 星期二 12时39分34秒
 ************************************************************************/

#include<iostream>
#include<vector>
#include<map>
using namespace std;


//Return true if word1 and word2 are the same length.
//and differ in only one character

bool oneCharOff(const string &word1,const string &word2)
{
    if(word1.length()!=word2.length())
       return false;
    int diffs=0;

    for(int i=0;i<word1.length();i++)
     if(word1[i]!=word2[i])
       if(++diffs>1)
          return false;

    return diffs=1;
}


map<string,vector<string>> computeAdjacentWords(const vector<string> &words)
{
    map<string,vector<string>> adjWords;
    map<int,vector<string>> wordsByLength;

    //Group by their length.
    for(int i=0;i<words.size();i++)
    {
        wordsByLength[words[i].length()].push_back(words[i]);
    }

    //word on each group separately

    map<int,vector<string>>::const_iterator itr;
    for(itr=wordsByLength.begin();itr!=wordsByLength.end();++itr)
    {
        const vector<string> &groupWords=itr->second;
        for(int i=0;i<groupWords.size();i++)
          for(int j=i+1;j<groupWords.size();j++)
            if(oneCharOff(groupWords[i],groupWords[j]))
           {
               adjWords[groupWords[i]].push_back(groupWords[j]);
               adjWords[groupWords[j]].push_back(groupWords[i]);

           }
    }

    return adjWords;

}

//Compute a map in which the keys in words and values are vectors of word
//that differ in only one character from the corresponding key.
//Uses an efficient algorithm that is O(NlogN) with a map
map<string,vector<string>> computeAdjacentWords_2(const vector<string> &words)
{
    map<string,vector<string>> adjWords;
    map<int,vector<string>> wordsByLength;
    for(int i=0;i<words.size();i++)
    {
        wordsByLength[words[i].length()].push_back(words[i]);
    }

    for(auto itr= wordsByLength.begin();itr!=wordsByLength.end();++itr)
    {
        const vector<string> &groupWords=itr->second;
        int groupNum=itr->first;
        for(int i=0;i<groupNum;i++)
        {
            map<string,vector<string>> repToWord;

            for(int j=0;j<groupWords.size();j++)
            {
                string rep=groupWords[j];
                rep.erase(i,1);
                repToWord[rep].push_back(groupWords[j]);
            }


            for(auto itr2=repToWord.begin();itr2!=repToWord.end();itr2++)
            {
                const vector<string> &clique=itr2->second;
                for(int p=0;p<clique.size();p++)
                {
                    for(int q=p+1;q<clique.size();q++)
                    {
                        adjWords[clique[p]].push_back(clique[q]);
                        adjWords[clique[q]].push_back(clique[p]);
                    }
                }
            }

        }
        
    }

    return adjWords;
    
}



int main()
{
    string s;
    vector<string> vec;
    while(cin>>s)
      vec.push_back(s);

    map<string,vector<string>> AdjWords=computeAdjacentWords_2(vec);

    for(auto itr=AdjWords.begin();itr!=AdjWords.end();itr++)
    {
        cout<<itr->first<<" have "<<itr->second.size()<<" items:"<<endl;
        for(int i=0;i<itr->second.size();i++)
          cout<<itr->second[i]<<" ";
        cout<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值