题目链接

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. 全面理解python中self的用法

    self代表类的实例,而非类. class Test: def prt(self): print(self) print(self.__class__) t = Test() t.prt() 执行结果 ...

  2. 多项式模板&题目整理

    注:多项式的题目,数组应开:N的最近2的整数次幂的4倍. 多项式乘法 FFT模板 时间复杂度\(O(n\log n)\). 模板: void FFT(Z *a,int x,int K){ static ...

  3. LoadRunner中字符串的操作

    LoadRunner中字符串的操作 LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string); ...

  4. leetcode-105-从前序与中序遍历构造二叉树

    题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.va ...

  5. Oracle - 用户及表空间的创建和删除

    -- 查询所有用户 SELECT USERNAME FROM ALL_USERS; -- 查询所有表空间 SELECT TABLESPACE_NAME FROM USER_TABLESPACES; - ...

  6. 8年前诞生于淘宝,细数阿里云RPA 的前世今生!

    9月10日,踏入55岁的马云正式卸任阿里巴巴董事局主席一职,由阿里巴巴集团CEO张勇接任.公寓创业.西湖论剑.美国敲钟,从成立到登顶中国最值钱的公司,阿里巴巴只用了20年. 阿里云RPA,2011年诞 ...

  7. 阿里巴巴飞天大数据架构体系与Hadoop生态系统

    很多人问阿里的飞天大数据平台.云梯2.MaxCompute.实时计算到底是什么,和自建Hadoop平台有什么区别. 先说Hadoop 什么是Hadoop? Hadoop是一个开源.高可靠.可扩展的分布 ...

  8. Ubuntu下使用SSH 命令用于登录远程桌面

    https://blog.csdn.net/yucicheung/article/details/79427578 问题描述 做DL的经常需要在一台电脑(本地主机)上写代码,另一台电脑(服务器,计算力 ...

  9. 移动端自定义输入框的vue组件 ----input

    <style scoped lang="less"> .keyboard { font-family: -apple-system, BlinkMacSystemFon ...

  10. 项目接入即时聊天客服系统(环信系统)PHP后端操作

    环信工作原理: 一.由于环信没有直接的接口来主动调取本项目中的用户数据,所有用户信息必须在环信服务器上注册对应信息成为环信的用户:(这样才能当用户进入聊天时显示其基本信息,如:名称.昵称.电话.邮箱等 ...