问题:确定一篇文章中的所有单词是否可以组成一个特定的句子?
解决方法:把文章中的所有单词放入<单词、单词出现次数>的hash_map中,单词是关键词。把句子中的所有单词放入<单词,单词出现次数>类型的hash_map中,单词是关键词。然后比较每个在句子中出现的单词的出现次数与相应单词在文章中出现次数,若有在句子中出现的单词的出现次数比相应单词在文章中出现次数多的就说明文章中出现的单词不够构成这个句子,否则可以。
以下C++代码可供参考:
#include<stdio.h>
#include<hash_map>
#include<string>
#include<iostream>
using namespace std;
using stdext::hash_map;
int main()
{
char buf[20];
int n;
FILE* filestream_paper;
FILE* filestream_sentence;
string word_temp;
stdext::hash_map<string,int>::iterator word_iterator_in_paper;
stdext::hash_map<string,int>::iterator word_iterator_in_sentence;
filestream_paper=fopen("newspaper_like.txt","r");
if(filestream_paper==NULL)
{
printf("open file error\n");
return -1;
}
hash_map<string, int> word_count_in_paper;
while(n=fs