题目链接

D. Fedor and Essay
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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.

Input

The 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.

Output

Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.

Sample test(s)
input
3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
output
2 6
input
2
RuruRu fedya
1
ruruRU fedor
output
1 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的更多相关文章

  1. 【Codeforces 467D】Fedor and Essay

    Codeforces 467 D 题意:给\(m​\)个单词,以及\(n​\)个置换关系,问将\(m​\)个单词替换多次后其中所含的最少的\(R​\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...

  2. Codeforces 467D Fedor and Essay bfs

    题目链接: 题意: 给定n个单词. 以下有m个替换方式.左边的单词能变成右边的单词. 替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短 输出字母r的个数和单词长度. 思路: 我们觉得 ...

  3. 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 ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. JavaScript特效源码(3、菜单特效)

    1.左键点击显示菜单 左键弹出式菜单[推荐][修改显示的文字及链接即可][共2步] ====1.将以下代码加入HEML的<head></head>之间: <style t ...

  2. 9.SpringJDBC模板类

    1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2. 提供了JDBC模板,Spring框架提供的 * JdbcTemplate类 3. Spring框架可以整 ...

  3. day09 samba、nginx服务配置

    samba 1.环境准备 [root@localhost ~]# iptables -F #清除防火墙配置 [root@localhost ~]# systemctl stop firewalld # ...

  4. 转:Eclipse中设置编码的方式

    来源:http://blog.csdn.net/jianw2007/article/details/3930915 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Ja ...

  5. 软件工程(C编码实践篇)学习总结

    吴磊 SA17225400 学习完了孟老师的软件工程(C编码实践篇),跟着老师一步步的完成了代码的编写,收获真的很大. 在学习这门课之前,我还不会用Linux,也没接触过Git.如今我已成能够在Lin ...

  6. momentjs 使用总结

    一.安装 cnpm install moment 二.引入 var moment = require('moment') 三.使用 let today = moment().format('YYYY- ...

  7. layui -关闭窗口方法

    1.关闭当前窗口 var index=parent.layer.getFrameIndex(window.name); //获取当前窗口的nameparent.layer.close(index); ...

  8. csp-s模拟47 Emotional Flutter,Endless Fantasy题解

    题面:https://www.cnblogs.com/Juve/articles/11558523.html A:Emotional Flutter 如果起点确定,那么我们后面走的点都是固定的,及mo ...

  9. Ionic 微信支付

    1.安装插件 ionic plugin add https://github.com/mrwutong/cordova-qdc-wxpay.git 2.代码 controller.js angular ...

  10. Tuxera ntfs软件如何删除干净

    sudo /Library/Filesystems/fusefs_txantfs.fs/Contents/Resources/Support/uninstall-package.sh