首页 > 代码库 > hdu 2572 优先队列
hdu 2572 优先队列
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#define max_one 100
#define max_two 50
#define max_three 50
using namespace std;
struct node //structure must sign first
{
string str;
int size;
};
struct cmp //return the most minimum string
//pay attention :
//must complete size first then complete string
{
bool operator() (const node &a, const node &b)
{
if(a.size >b.size)
return true;
else if(a.size ==b.size)
return a.str>b.str;
else return false;
}
};
priority_queue <node, vector<node>, cmp> que;
void score_string(char *p, char *q, int two, int three) //score string into queue
{
string temp="";
if(p>q)
{
int length=three > p-q+two ? three : p-q+two;
for(int i=0; i<length ;i++)
temp+=*(q+i);
node a;
a.str=temp;
a.size=length;
que.push(a);
}
else if(p<q)
{
int length=two > q-p+three ? two : q-p+three;
for(int i=0; i<length ;i++)
temp+=*(p+i);
node a;
a.str=temp;
a.size=length;
que.push(a);
}
else if(p=q) //pay attention : p=q such as this string
//one :asdfgh two :asdfgh three:a;
{
int length=two >three ? two :three;
for(int i=0; i<length ;i++)
temp+=*(p+i);
node a;
a.str=temp;
a.size=length;
que.push(a);
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
while(!que.empty() ) //each text must clean the queue
{
que.pop();
}
char one[max_one],two[max_two],three[max_three];
cin>>one>>two>>three;
int two_length=strlen(two); //score size of two three string
int three_length=strlen(three);
char *p=strstr(one,two);
char *q=strstr(one,three);
if(p==NULL ||q==NULL)
cout<<"No"<<endl;
else
{
char *two_point[max_two]; //set pointer array score the possible way of string
char *three_point[max_three];
two_point[0]=p;three_point[0]=q;
int size_two=1;
char *temp_two=p,*temp_three=q;
while( (temp_two=strstr(temp_two+1,two))!=NULL) //get different substring way
two_point[size_two++]=temp_two;
int size_three=1;
while( (temp_three=strstr(temp_three+1,three))!=NULL)
three_point[size_three++]=temp_three;
for(int i=0; i<size_two; i++) //score
{
for(int j=0; j<size_three; j++)
score_string(two_point[i], three_point[j], two_length, three_length);
}
cout<<que.top().str<<endl;
}
//cout<<que.top().str<<endl;
}
return 0;
}
来自为知笔记(Wiz)
附件列表
hdu 2572 优先队列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。