寒假训练——HDU - 2532 Engine 模拟

谷歌、百度等搜索引擎已经成为了互连网中不可或缺的一部分。在本题中,你的任务也是设计一个搜索论文的搜索引擎,当然,本题的要求比起实际的需求要少了许多。 
本题的输入将首先给出一系列的论文,对于每篇论文首先给出标题,然后给出它被引用的次数。然后会有一系列的搜索询问,询问标题中包含特定关键词的论文有哪些。 
每一个询问可能包含多个关键词,你需要找出标题包含所有关键词的论文。 
“包含”必须是标题中有一个词正好是给定的关键词,不区分大小写。 
对每个询问,都按被引用的次数从多到少输出满足条件的论文的标题。如果有被引用的次数相同的论文,则按照论文在输入中的顺序排列,先给出的论文排在前面。 
 
Input
输入包含多组数据。 
每组数据首先有一行包含一个整数N(1<=N<=1000),表示论文的数目,N=0表示输入结束。每组论文的信息第一行是论文的标题,由字母(大小写均可)和空格组成,不超过10个词,每个词不超过20个字符,标题总共不超过250个字符。第二行是一个整数K(0<=K<=108),表示它被引用的次数。在论文信息结束以后,有一行包含一个整数M(1<=M<=100),表示询问的数目。接下来有M行,每行是一个询问,由L(1<=L<=10)个空格分开的词构成,每个词不超过20个字符。 

Output
对每个询问,按照题目给定的顺序输出满足条件的论文的标题;如果没有满足条件的论文,就不输出。在每组询问的输出之后输出一行”***”,在每组数据的输出之后输出一行”---”。
Sample Input
6
Finding the Shortest Path
120
Finding the k Shortest Path
80
Find Augmenting Path in General Graph
80
Matching in Bipartite Graph
200
Finding kth Shortest Path
50
Graph Theory and its Applications
40
6
shortest path
k shortest path
graph
path
find
application
0
Sample Output
Finding the Shortest Path
Finding the k Shortest Path
Finding kth Shortest Path
***
Finding the k Shortest Path
***
Matching in Bipartite Graph
Find Augmenting Path in General Graph
Graph Theory and its Applications
***
Finding the Shortest Path
Finding the k Shortest Path 
Find Augmenting Path in General Graph
Finding kth Shortest Path
***
Find Augmenting Path in General Graph
***
***
---



先根据访问次数用稳定排序算法排序

然后利用set存标题单词,句子分词,遍历搜索

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

typedef struct node{
	string ss;
	int val;
}NODE; 

bool cmp(NODE x,NODE y)
{
	return x.val>y.val;
}



vector<NODE> pass;

vector< set<string> >  a;
int n,m;
int re[1005];
//set<int> re;
//set<int>::iterator it;
vector<int> val;
set <string> tt;

int main()
{
	string st,x;
    while(getline(cin,st))
    {
    	n = atoi(st.c_str());
    	if(n==0)  break;
    	val.clear();
		pass.clear();
		a.clear();
		tt.clear();
	
		for(int i=0;i<n;i++)
		{
			a.push_back(tt);
		}
		for(int k=0;k<n;k++)
		{
			NODE t;
			getline(cin,st);
		
			t.ss = st;
			getline(cin,x);
			t.val = atoi(x.c_str());
			pass.push_back(t);
		}
		
		/*for(int i=0;i<pass.size();i++)
		{
			for(int j=i+1;j<pass.size();j++)
			{
				NODE t;
				if(pass[i].val<pass[j].val)
				{
					t = pass[i];
					pass[i] = pass[j];
					pass[j] = t;
				}
			}
		}*/
		stable_sort(pass.begin(),pass.end(),cmp);

    	for(int k=0;k<n;k++)
    	{
			st = pass[k].ss;
			
			for(int i=0;i<st.size();i++)
			{
				if(st[i]>='A'&&st[i]<='Z')
				{
					st[i] +=32;
				}
			}	
			st+=" ";
			string word; 
			word.clear();
			for(int i=0;i<st.size();i++)
			{
			
				if(st[i]==' ')
				{
					if(word.size()!=0)
					{
						word+='\0';
				//		cout <<word<<endl;
						a[k].insert(word);
						word.clear();
					}
					

				}
				else
				{
					word+=st[i];
				}
			}
		}
		
		
		getline(cin,x);
		m = atoi(x.c_str());
		for(int k=0;k<m;k++)
		{
			memset(re,0,sizeof(re));
			getline(cin,st);
			for(int i=0;i<st.size();i++)
			{
				if(st[i]>='A'&&st[i]<='Z')
				{
					st[i] +=32;
				}
			}
			int ct11=0;
			st+=" ";
			string word; 
			word.clear();
			for(int i=0;i<st.size();i++)
			{
				if(st[i]==' ')
				{
					if(word.size()!=0)
					{
						ct11++;
						word+='\0';
						for(int j=0;j<n;j++)
						{
							if(a[j].count(word)>0)
								re[j]++;
						}
						word.clear();
					}
					
				}
				else
				{
					word+=st[i];
				}
			}
			
			
			for(int i=0;i<n;i++)
			{
				if(re[i]==ct11)
				{
					cout<<pass[i].ss<<endl;
				}
					
			}
			cout<<"***"<<endl; 
		}
		cout<<"---"<<endl;
    }    
} 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值