uvalive 6680 - Join the Conversation 动态规划

来源:互联网 发布:程序员编程语言 编辑:程序博客网 时间:2024/04/30 04:41

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4692


这个题真是汗死了,训练赛的时候我看这个题快看了三个小时,题意到最后还是觉得没有理解,结果看人别人写的就是一个dp而已。

题意:  要找到一个最大的集合,包含最多的行数。如果后面的行的正文部分  包含前面行 的作者,那么这两句话可以放到一个集合里。


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   num<<1,le,mid#define rson    num<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn=50000+20    ;//const int maxm=    ;int n;int dp[maxn],pre[maxn];string s,message,name,word;map<string ,pair<int ,int > >mp;vector<int > ve;int main(){    while(~scanf("%d",&n))    {        getchar();        mp.clear();        for(int i=1;i<=n;i++)        {            getline(cin,s);            stringstream message(s);            message>>name;            name=name.substr(0,name.length()-1);            dp[i]=1;            pre[i]=-1;            while(message>>word)            {                if(!mp.count(word)||word==name)   continue;                if(mp[word]._f+1>dp[i])  dp[i]=mp[word]._f+1,pre[i]=mp[word]._s;            }            if(!mp.count(name ) )  mp[name]=mk(dp[i],i);            if(dp[i]>mp[name]._f)    mp[name]._f=dp[i],mp[name]._s=i;        }        int p=1,maxi=1;        for(int i=1;i<=n;i++)        {            if(dp[i]>maxi )  maxi=dp[i],p=i ;        }        ve.clear();        while(~p)        {            ve.push_back(p );            p=pre[p];        }        printf("%d\n",ve.size());        if(ve.size()==0)  continue;        for(int i=ve.size()-1;i>=1;i--)        {            printf("%d ",ve[i]);        }        printf("%d\n",ve[0]);    }    return 0;}



0 0
原创粉丝点击