UVALive 6680Join the Conversation
来源:互联网 发布:网络在线测速 编辑:程序博客网 时间:2024/05/16 12:18
题意:给出n条发言,每@一次为一次交流,求最长的连续交流长度,并按顺序输出。前面的人不能@后面的人。
分析:对于每一层,更新到这一层的最大长度,并记录下父节点。
细节参见代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<map>#include<cmath>#include<algorithm>#include<queue>#include<stack>#include<sstream>using namespace std;const int maxn=50000+10;int n;stack<int> s;string buf,str,name;struct node{ int d,p; node(int u=0,int v=0) :d(u),p(v) {}};map<string,node> mp;int dp[maxn],pre[maxn];int main(){ while(scanf("%d",&n)==1) { getchar(); mp.clear(); for(int i=1;i<=n;i++) { dp[i]=1; pre[i]=-1; getline(cin,str); stringstream ss(str); ss>>name; name.resize(name.size()-1); if(!mp.count(name)) { node u(1,i); mp[name]=u; } while(ss>>buf) { if(!mp.count(buf)||buf==name) continue; if(mp[buf].d+1>dp[i]) { dp[i]=mp[buf].d+1; pre[i]=mp[buf].p; } } if(mp[name].d<dp[i]) { mp[name]=node(dp[i],i); } } int p=1,ans=0; for(int i=1;i<=n;i++) if(dp[i]>ans) { p=i; ans=dp[i]; } while(p!=-1) { s.push(p); p=pre[p]; } printf("%d\n",ans); int is=1; while(s.size()) { if(is) {printf("%d",s.top());is=0;} else printf(" %d",s.top()); s.pop(); } printf("\n"); } return 0;}
0 0
- UVALive 6680 Join the Conversation
- UVALive 6680Join the Conversation
- uvalive 6680 - Join the Conversation 动态规划
- UVALive 6680 Join the Conversation (DP)
- Join the Conversation
- 1880: Join the Conversation
- UVALive 3637 The Bookcase
- UVALive - 3637 The Bookcase
- UVALive 6937 The Imp
- Paint the Roads UVALive
- Save the Students! UVALive
- Cross the Wall UVALive
- UVALive 3942 Remember the Word
- UVALive - 3693 Balancing the Scale
- UVALive 6741 The Sacrificial Firepits
- UVALive - 6322 The Swapping Game
- UVALive 5072 Find the Multiples
- UVALive 6436 The Busiest City
- Hello World!
- 最简单的基于FFmpeg的移动端例子:Android 视频解码器-单个库版
- FastDFS配置文件操作说明
- OWASP安全编码规范快速参考的术语
- Swift---一门智能型的编程语言
- UVALive 6680Join the Conversation
- String 变量只要被赋值就等于新new一个String对象
- C++溢出对象虚函数表指针
- ArrayList的默认初始容量及扩容
- LightOJ 1085
- JavaScript事件代理和委托(Delegation)
- 让sql语句不排序,按照in语句的顺序返回结果
- 51nod 1268 和为k的组合
- [LeetCode]344. Reverse String