UVALive 6680 Join the Conversation
题意:conversion的定义是下一句提到上一句的人的名字。请你输出最长的对话的长度,及组成对话的序列号。
思路:动态规划的思想很容易想到,当前句子,根据所有提到的人的名字为结尾组成的对话长度来判断当前name的最长对话长度。每个名字需要记录它形成最长对话的长度及此时出现的行数。
想说的是,C++处理起来好优雅。Cplusplusdeeplover.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <sstream>
#define maxn 50010
#include <map>
using namespace std; int dp[maxn], pre[maxn];
string mess, word, name, str; map<string, pair<int, int> >mp; bool ok; void print(int num) {
if (num == -1) return;
print(pre[num]);
if (ok) {
printf("%d", num+1);
ok = false;
}
else printf(" %d", num+1);
return;
} int main() {
//freopen("in.cpp", "r", stdin);
int n;
while(cin >> n) {
getchar();
mp.clear();
for (int i=0; i<n; ++i) {
getline(cin, str);
//cout << str << "...\n";
stringstream mess(str);
mess >> name;
name = name.substr(0, name.length()-1);
dp[i] = 1;
pre[i] = -1;
while(mess>>word) {
if (!mp.count(word) || word == name) continue;
if (mp[word].first + 1 > dp[i]) {
dp[i] = mp[word].first + 1;
pre[i] = mp[word].second;
}
}
if (!mp.count(name)) mp[name] = make_pair(dp[i], i);
if (mp[name].first < dp[i]) mp[name].first = dp[i], mp[name].second = i;
} int maxdp = -1, pos;
ok = true;
for (int i=0; i<n; ++i) {
if (maxdp < dp[i]) {
maxdp = dp[i];
pos = i;
}
}
cout << maxdp << endl;
print(pos);
cout << endl;
}
return 0;
}
UVALive 6680 Join the Conversation的更多相关文章
- UVaLive 6680 Join the Conversation (DP)
题意:给出n条发言,让你求最大的交流长度并输出标记顺序. 析:这个题要知道的是,前面的人是不能at后面的人,只能由后面的人at前面的,那就简单了,我们只要更新每一层的最大值就好,并不会影响到其他层. ...
- chrome49 新特性 chrome.org转载
Transitioning from SPDY to HTTP/2 Thursday, February 11, 2016 Last year we announced our intent to e ...
- DEEP LEARNING WITH STRUCTURE
DEEP LEARNING WITH STRUCTURE Charlie Tang is a PhD student in the Machine Learning group at the Univ ...
- 写出完美论文的十个技巧10 Tips for Writing the Perfect Paper
10 Tips for Writing the Perfect Paper Like a gourmet meal or an old master painting, the perfect col ...
- The Sorrows of Young Werther
The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...
- The 2013 South America/Brazil Regional Contest 题解
A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...
- ML.NET is an open source and cross-platform machine learning framework
https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet Machine Learning made for ...
- video conference s/w
CamFrogWindows | Mac OS | Linux (Server only) | iOS | Android | Windows PhoneCamFrog lets you set up ...
- jms版本
Java消息服务是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914). 2001年6月25日,Java消息服务发布JMS 1.0.2b,2002年3月18日Java消息服务发布 1 ...
随机推荐
- iOS - OC Copy 拷贝
前言 copy:需要先实现 NSCopying 协议,创建的是不可变副本. mutableCopy:需要实现 NSMutableCopying 协议,创建的是可变副本. 浅拷贝:指针拷贝,源对象和副本 ...
- c++ 临时变量
C++的临时变量 它们是被神所遗弃的孩子,没有人见过它们,更没有人知道它们的名字.它们命中注定徘徊于命运边缘高耸的悬崖和幽深的深渊之间,用自己短暂的生命抚平了生与死之间的缝隙.譬如朝露,却与阳光无缘. ...
- windos多线程编程
随机数滚动发生器 #include <stdio.h> #include <Windows.h> #include <ctime> #include <pro ...
- Object Pascal 数据类型
数据类型与定义变量 Object Pascal 语言的最大特点是对数据类型的要求非常严谨.传递给过程或函数的参数值必须与形参的类型一致.在Object Pascal 语言中不会看到像C 语言编译器提 ...
- git的作用和原理(待续)
先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令: $ sudo git init --bare sample.git Git就会创建一个裸仓库,裸仓库没有 ...
- centos JDK安装
第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...
- jquery中html(), text(),val()区别(zhuan)
https://zhidao.baidu.com/question/307317838.html http://www.cnblogs.com/aqbyygyyga/archive/2011/11/0 ...
- 别人要访问我的电脑上部署的tomcat,必须关闭防火墙吗?
局域网内要访问服务器上部署的tomcat,必须关闭防火墙吗? 不一定. 如果是需要使用 IP:端口号(ip:port)来访问,可以做以下设置(这里仅是说的tomcat访问). 首先在服务器的控制面板中 ...
- spring 编程式事务管理和声明式事务管理
编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...
- aspx控件属性
ASPxGridView属性:概述设置(Settings) <Settings GridLines="Vertical" : 网 ...