题意:给出n条发言,让你求最大的交流长度并输出标记顺序。

析:这个题要知道的是,前面的人是不能at后面的人,只能由后面的人at前面的,那就简单了,我们只要更新每一层的最大值就好,并不会影响到其他层。

最后再从这 n 层中取出最大值,在更新时,也可以记录着最大值。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <sstream>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 5;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<string, P> mp;
int dp[maxn], pre[maxn]; void print(int pos){
if(!pre[pos]){ printf("%d", pos); return; }
print(pre[pos]);
printf(" %d", pos);
} int main(){
while(scanf("%d", &n) == 1){
string line, s;
getchar();
mp.clear();
for(int i = 1; i <= n; ++i){
getline(cin, line);
stringstream ss(line);
ss >> s;
string str = s.substr(0, s.size()-1);
dp[i] = 1, pre[i] = 0;
while(ss >> s){
if(!mp.count(s) || s == str) continue;
if(mp[s].first + 1 > dp[i]){
dp[i] = mp[s].first + 1;
pre[i] = mp[s].second;
}
}
if(mp[str].first < dp[i]) mp[str] = P(dp[i], i);
} int ans = 0, pos;
for(int i = 1; i <= n; ++i)
if(dp[i] > ans) ans = dp[i], pos = i;
printf("%d\n", ans);
print(pos);
printf("\n"); }
return 0;
}

UVaLive 6680 Join the Conversation (DP)的更多相关文章

  1. UVALive 6680 Join the Conversation

    题意:conversion的定义是下一句提到上一句的人的名字.请你输出最长的对话的长度,及组成对话的序列号. 思路:动态规划的思想很容易想到,当前句子,根据所有提到的人的名字为结尾组成的对话长度来判断 ...

  2. UVALive 4261——Trip Planning——————【dp+打印路径】

    Trip Planning Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  3. UVALive 6491 You win! 状态DP

    这个题目上周的对抗赛的,美国2013区域赛的题目,上次比赛真惨,就做出一道题,最多的也只做出两道,当时想把这题做出来,一直TLE. 这个题目用挂在Hunnu OJ的数据可以过,但UVALive上死活过 ...

  4. UVaLive 7143 Room Assignment (组合数+DP)

    题意:有 n 个客人,m个房间,每个房间可住ci个人,这 n 个人中有 t 对双胞胎,sum{ci}  = n 问你有多少种住房方法. 析:计数DP,dp[i][j] 表示前 i 个房间,还剩下 j ...

  5. UVALive 4987---Evacuation Plan(区间DP)

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

  6. UVALive 5066 Fire Drill --BFS+DP

    题意:有一个三维的地图,有n个人被困住,现在消防队员只能从1楼的一个入口进入,营救被困者,每一个被困者有一个价值,当消防队员找到一个被困者之后,他可以营救或者见死不救,如果救的话,他必须马上将其背到入 ...

  7. UVALive 7276 Wooden Signs (DP)

    Wooden Signs 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/E Description http://7xjob4. ...

  8. uvalive 5721 Activation (概率dp+方程)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=24999 主要思想就是解方程的思想. 二维dp应该很容易想到,就是当前位置加队伍长度. dp ...

  9. UVALive 6672 Bonus Cards 概率dp

    题意呢 就是有两种售票方式 一种是icpc 一种是其他方式 icpc抢票成功的概率是其他方式的2倍…… 这时 一个人出现了 他通过内幕知道了两种抢票方式各有多少人 他想知道自己如果用icpc抢票成功的 ...

随机推荐

  1. 13-看图理解数据结构与算法系列(Trie树)

    Trie树 Trie树,是一种搜索树,也称字典树或单词查找树,此外也称前缀树,因为某节点的后代存在共同的前缀.它的key都为字符串,能做到高效查询和插入,时间复杂度为O(k),k为字符串长度,缺点是如 ...

  2. 11-看图理解数据结构与算法系列(B树的删除)

    删除操作 删除操作比较复杂,主要是因为删除的项可能在叶子节点上也可能在非叶子节点上,而且删除后可能导致不符合B树的规定,这里暂且称之为导致B树不平衡,于是要进行一些合并.左旋.右旋等操作,使之符合B树 ...

  3. 标准sqlserver连接语句

    sqlserver左右全内连接 原始链接http://www.cnblogs.com/youzhangjin/archive/2009/05/22/1486982.html      连接条件可在FR ...

  4. EGit应用

    [创建Dynamic Web Project项目] [创建仓库] 项目(鼠标右键) ==〉Team==〉Share Project..... ==〉选择Git 配置Repository的目录 创建完成 ...

  5. oracle基于归档的增量异地恢复 --异地新增数据文件问题

    1 rac异地基于归档文件的增量恢复   2 遇到在主库新增数据文件 后 日志恢复出现路径问题 .   错误如下 : creating datafile fno=20 name=+DATA/dataf ...

  6. PHP关于文件与文件夹(1) 写入文件 文件权限 三、锁定文件

    一.文件权限 总之中的一个切都是为了保证文件夹的安全,保证文件夹的安全比保证文件的安全更重要. 二.写入文件 file_put_contents($file,$data);  //假设没有的话会创建. ...

  7. Unix时间戳(Unix timestamp)转换

    http://tool.chinaz.com/Tools/unixtime.aspx 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScr ...

  8. storm ——Understanding the Parallelism of a Storm Topology

    http://www.michael-noll.com/blog/2012/10/16/understanding-the-parallelism-of-a-storm-topology/ 这篇文章好 ...

  9. SATA、SCSI、SAS

    目前,服务器市场上采用的硬盘主要有三种,SATA硬盘.SCSI硬盘以及SAS硬盘,其中SATA硬盘主要应用在低端服务器领域,而SCSI和SAS硬盘则面向中高端服务器.下面我们就SATA.SCSI以及S ...

  10. C#中Stack&lt;T&gt;类的使用及部分成员函数的源代码分析

    Stack<T>类 Stack<T> 作为数组来实现. Stack<T> 的容量是 Stack<T> 能够包括的元素数. 当向 Stack<T&g ...