Codeforces 467D
D. Fedor and Essaytime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language.
Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times.
As a result, Fedor wants to get an essay which contains as little letters «R» (the case doesn't matter) as possible. If there are multiple essays with minimum number of «R»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay.
Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG.
InputThe first line contains a single integer m (1 ≤ m ≤ 105) — the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters.
The next line contains a single integer n (0 ≤ n ≤ 105) — the number of pairs of words in synonym dictionary. The i-th of the next nlines contains two space-separated non-empty words xi and yi. They mean that word xi can be replaced with word yi (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5·105 characters.
All the words at input can only consist of uppercase and lowercase letters of the English alphabet.
OutputPrint two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.
Sample test(s)input3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr youtput2 6input2
RuruRu fedya
1
ruruRU fedoroutput1 10
/*************************************************************************
> File Name: D.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月19日 星期五 14时41分44秒
> Propose:
************************************************************************/
#include <map>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = ;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
typedef long long LL; int V; // 顶点数
vector<int> G[MAX_N], rG[MAX_N], vs;
bool used[MAX_N];
int cmp[MAX_N];
//题目变量
map<string, int> HASH;
int n, m, ID[MAX_N], X[MAX_N], Y[MAX_N];
pii s[MAX_N], ss[MAX_N], dp[MAX_N]; void add_edge(int from, int to) {
G[from].push_back(to);
rG[to].push_back(from);
} void dfs(int v) {
used[v] = true;
for (int i = ; i < G[v].size(); i++) {
if (!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
} void rdfs(int v, int k) {
used[v] = true;
cmp[v] = k;
ss[k] = min(ss[k], s[v]);
for (int i = ; i < rG[v].size(); i++) {
if (!used[rG[v][i]]) rdfs(rG[v][i], k);
}
} int scc() {
memset(used, false, sizeof(used));
vs.clear();
for (int v = ; v <= V; v++) {
if (!used[v]) dfs(v);
}
memset(used, false, sizeof(used));
int k = ;
for (int i = vs.size() - ; i >= ; i--) {
if (!used[vs[i]]) {
ss[++k] = pii(INF, INF);
rdfs(vs[i], k);
}
}
return k;
} int get(string &str) {
for (int i = ; i < str.size(); i++) {
str[i] = tolower(str[i]);
}
if (HASH.find(str) == HASH.end()) {
HASH[str] = ++V;
s[V].second= str.size();
for (int j = ; j < s[V].second; j++) if (str[j] == 'r') s[V].first++;
return V;
} else {
return HASH[str];
}
} void rebuild() {
for (int i = ; i <= V; i++) G[i].clear();
for (int i = ; i <= m; i++) if (cmp[X[i]] != cmp[Y[i]])
add_edge(cmp[X[i]], cmp[Y[i]]);
} pii DFS(int u) {
if (used[u]) return dp[u];
used[u] = true;
dp[u] = ss[u];
for (int i = ; i < G[u].size(); i++) {
dp[u] = min(dp[u], DFS(G[u][i]));
}
return dp[u];
} int main(void) {
ios::sync_with_stdio(false);
cin >> n;
for (int i = ; i <= n; i++) {
string str;
cin >> str;
int id = get(str);
ID[i] = id;
} cin >> m;
for (int i = ; i <= m; i++) {
string x, y;
cin >> x >> y;
int u = get(x), v = get(y);
add_edge(u, v);
X[i] = u, Y[i] = v;
} int k = scc();
rebuild(); memset(used, false, sizeof(used));
LL resr = , resl = ;
for (int i = ; i <= n; i++) {
int pos = cmp[ID[i]];
DFS(pos);
resr += dp[pos].first;
resl += dp[pos].second;
} cout << resr << ' ' << resl << endl;
return ;
}
Codeforces 467D的更多相关文章
- 【Codeforces 467D】Fedor and Essay
Codeforces 467 D 题意:给\(m\)个单词,以及\(n\)个置换关系,问将\(m\)个单词替换多次后其中所含的最少的\(R\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...
- Codeforces 467D Fedor and Essay bfs
题目链接: 题意: 给定n个单词. 以下有m个替换方式.左边的单词能变成右边的单词. 替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短 输出字母r的个数和单词长度. 思路: 我们觉得 ...
- CodeForces 467D(267Div2-D)Fedor and Essay (排序+dfs)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- [转]Hessian——轻量级远程调用方案
Hessian是caucho公司开发的一种基于二进制RPC协议(Remote Procedure Call protocol)的轻量级远程调用框架.具有多种语言的实现,但用的最多的当然是Java实现 ...
- 基础回顾: 关于Session的一些细节
1 session是服务端技术, cookie是客户端技术 2 默认情况下, 一个浏览器独占一个session对象, 也就是说, 开启两个浏览器进程, 它们之间使用的session不是同一个sessi ...
- postgresql数据库安装后的pgadmin4切换中文
如图所示
- day1-初识Python以及环境搭建
---恢复内容开始--- 为什么学习Python? 软件质量:python的可读性很强,易于理解,非常接近于人类的自然语言. 提高开发者效率:相当于C,C++和JAVA等编译/静态型语言,Python ...
- 二分图匹配——poj1469
关于本题二分图的匹配关系始终是加单向边用左边去匹配右边,match表示的是右边的人匹配的对应的左边的点 /* 关于本题二分图的匹配 链接的关系始终是单向边 用左边去匹配右边,match表示的是右边的人 ...
- LUOGU P1313 计算系数 (组合数学)
解题思路 比较简单的题,用二项式定理即可. #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- escape encodeURI和encodeURIComponent的区别
escape(与之对应->unescape) escape是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读.编码之后的效果是%XX或者%uXXXX这种形式 ...
- 上海第三产业增加值 占比GDP首破七成
上海第三产业增加值 占比GDP首破七成 2016年08月16日08:10 来源:新闻晨报 分享到: 不久前结束的ChinaJoy上,一家名为HYPEREAL的VR公司展台前,体验者的热情程度 ...
- LINK : fatal error LNK1104: 无法打开文件“ucrtd.lib”
先说解决方案: 选中项目->右键->属性->常规 -->Windows SDK 改成当前系统的SDK版本,我这边是10.0.15063.0,重新生成即可 下载cefsh ...
- 07_Spring事务处理
一.事务概述 数据库的事务: 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等.事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出 ...