首页 技术 正文
技术 2022年11月20日
0 收藏 953 点赞 4,670 浏览 1108 个字

题意: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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844