我见过一个外企C++面试题,我当时也没做出来,经过这几天思考,有了点思路,现在给大家分享。注意,原题是英文的。
题目:给一个文本文件,该文件中包含一篇文章,要求写代码统计出里面长度最长的10个单词,并且记录打印出该单词所在的段落以及内容。
我刚看到题目的时候,认为这是一个词频统计的问题,所以想了各种方法都达不到好的效果,经过这几天思考,终于写出来这个题目的解法,特记录如下。
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <map>
using namespace std;
struct word
{
string content;
int graph;
word(string c,int g):content(c),graph(g){}
};
int cmp(word p1,word p2)
{
return p2.content.size() < p1.content.size();
}
int main()
{
fstream fs("d:/demo.txt");
fs >> noskipws;
char c; // each character.
string str; //each word
long pos=1; // graph
vector<word> vect; // all word.
map<int,string> graph; // graph,string mapping.
string str_graph;
while(!fs.eof())
{
fs>>c;
if(fs.eof())break;
if(c == '\n')continue;
if(c == ' ' || c == ',' || c == '.' || c == ':')
{
word w(str,pos);
vect.push_back(w);
str.clear();
str_graph.append(&c,1);
if(c == '.')
{
graph.insert(make_pair(pos,str_graph));
str_graph.clear();
}
}else{
str.append(&c,1);
str_graph.append(&c,1);
}
if(c == '.')
{
++pos;
}
}
sort(vect.begin(),vect.end(),cmp);
vector<word>::iterator begin = vect.begin();
while(begin!=vect.end())
{
word wo = *begin;
cout << wo.content << "--------------->" << wo.graph << endl;
++begin;
}
map<int,string>::iterator begin1 = graph.begin();
while(begin1!= graph.end())
{
pair<int,string> pa = *begin1;
cout << pa.first << "----------->" << pa.second << endl;
++ begin1;
}
return 0;
}
逐个字符进行读取,并过滤掉其中的标点符号,另外,专门声明一个struct用来记录这个单词以及单词对应的段落编号。在逐个字符读取时,同时也记录每一个段落的内容。另外,我这里用了一个文件进行测试,文件内容如下:
hi guys:where are you.
haha, have a guess.
welcome,lucy,welcome to home.
我分析了一下,里面主要是需要创建一个对象,另外需要留意使用sort函数,且自己实现compare函数。 上面代码可以优化空间很大,仅供参考即可。
打印结果就不记录了,上面这个程序在我电脑上运行过,已经统计出来了。