单词行号统计(普通程序员与大厂程序员实现差别)

题目如上图
//普通程序员代码

// 解题思路:把输入文本存储到vector<string>中,循环遍历数组,把string进行切割,存入map<string,vector<int>>中保存word和row号,
// 遍历map打印结果。

#include<iostream>
#include<map>
#include<vector>
#include<string.h>
#include<string>
using namespace std;
int main(){
	//把输入放进vector<string>里,string保存一列
	int row = 1;
	std::vector<std::string> file;
	std::string temp="";
	char c;
	std::string word;
	c=getchar();
	while(c!=EOF)
	{
		//换行则存储当前列
		if(c=='\n')
		{
			file.push_back(temp);
			temp="";
		}else
        {
            temp = temp + c;
	    
        }	
        c=getchar();				
	}
	
	//把string进行切割,存入map<word,vector<int>>中
	//遍历完vector<string>
		
	//std::vector<std::string>::iterator iter;
	std::map<std::string, std::vector<int> > WordKeep;
	std::map<std::string,std::vector<int> > ::iterator WordIter;
	
	for(size_t index = 0;index < file.size(); index++)
	{
		const char* TempRow = (file[index]).c_str();			//待分解字符串
		char *p1 = new char[strlen(TempRow)+1];
		strcpy(p1,TempRow);
		char* delim = " "; 										//分隔符字符串
		char* p=strtok(p1,delim);								//第一次调用strtok
		if(p==NULL){
			cout<<"指针为空";
		}
		while(p!=NULL){											//当返回值不为NULL时,继续循环
			word = p;											//保存分解的字符串	
			WordIter = WordKeep.find(word);
			if(word !=" "){										//过滤掉空格
				if(WordIter == WordKeep.end())
				{
					std::vector<int> KeepVector;
					KeepVector.push_back(row);
					WordKeep.insert(std::make_pair(word,KeepVector));
				}else
				{
					WordIter->second.push_back(row);			//保存当前列
				}
			}
			p=strtok(NULL,delim);								//继续调用strtok,分解剩下的字符串
		}
		delete p1;
		row++;	
	}
	
	for(WordIter = WordKeep.begin(); WordIter != WordKeep.end(); ++WordIter)		//打印结果
	{
       // string temp = WordIter->first;
		cout<<WordIter->first<<" ";
		for(size_t i = 0;i<WordIter->second.size();i++)
		{
			cout<<WordIter->second[i]<<" ";
		}
		cout<<"\n";
	}
    system("pause");
	return 0;
}

大厂程序员代码

//在我的思路上进行优化。
#include<iostream>
#include<map>
#include<vector>
#include<string.h>
#include<string>
using namespace std;

struct Pair{
    std::string word;
    std::vector<int> rows;
};

std::vector<std::string> split(const std::string& str, const std::string& delim)
{
	std::vector<std::string> res;
	if("" == str)
	{
	    return res;
	}

	char * strs = new char[str.length() + 1] ;
	strcpy(strs, str.c_str());

	char * d = new char[delim.length() + 1];
	strcpy(d, delim.c_str());

	char *p = strtok(strs, d);
	while(p)
    {
		std::string s = p;
		res.push_back(s);
		p = strtok(NULL, d);
	}

	return res;
}

int main()
{
    std::map<std::string, std::vector<int> > word2Rows;
    typedef std::map<std::string, std::vector<int> >::iterator  Iter;

    std::string str;

    int rowCount = 0;

    while(getline(std::cin, str))
    {
        ++rowCount;
        std::vector<std::string> tempVec =split(str, " ");
        for(size_t i = 0; i < tempVec.size(); ++i)
        {
            Iter it = word2Rows.find(tempVec[i]);
            if (it == word2Rows.end())
            {
                std::vector<int> rowsVec;
                word2Rows.insert(std::make_pair(tempVec[i], rowsVec));
            }

            //todo单行也要去重

            it = word2Rows.find(tempVec[i]);
            if (it != word2Rows.end())
            {
                it->second.push_back(rowCount);
            }
        }
    }

    for(Iter it = word2Rows.begin(); it != word2Rows.end(); ++it)
    {
			printf("%s ", it->first.c_str());
        for(size_t i = 0; i < it->second.size(); ++i)
        {
            printf("%d ", (it->second)[i]);
        }
        printf("\n");
    }

    //todo 填入这个指定结构
    std::vector<Pair> answers;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值