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 n lines 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.

输入输出样例

输入样例#1:

3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
输出样例#1:

2 6
输入样例#2:

2
RuruRu fedya
1
ruruRU fedor
输出样例#2:

1 10


十分气愤!!!
不懂为什么他们机房互模成瘾,实在忍不了。 我太菜了,这么水的题都想不出来...
Noip怕是凉了。
首先我们可以把每个出现的字符串当做一个点,把转化关系当做边。
用一个pair类型存储每个字符串的$r$的个数和长度。
然后贪心地想,我们肯定是要让尽可能多的字符串有尽量少的r和尽量少的长度。
把所有字符串放进队列里bfs,类似spfa,如果能更新的话就再次入队。
最后所有更新完之后的1——n的r的个数和长度一定是最短的。
还要注意字符串会有重复,比较坑。
总之我太菜了。

 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <queue>
#include <map>
#include <vector>
using namespace std;
#define reg register
inline char gc() {
static const int BS = << ;
static unsigned char Buf[BS], *st, *ed;
if (st == ed) ed = Buf + fread(st = Buf, , BS, stdin);
return st == ed ? EOF : *st++;
}
#define gc getchar
inline int read() {
int res=;char ch=getchar();bool fu=;
while(!isdigit(ch)) {if(ch=='-')fu=;ch=getchar();}
while(isdigit(ch))res=(res<<)+(res<<)+(ch^), ch=getchar();
return fu?-res:res;
}
#define ll long long
#define pii pair <ll, ll>
#define mkp make_pair
int n, m;
map <string, int> mp;
int tot;
pii memc[];
vector <int> ve[];
ll ans1, ans2;
int cp[]; inline void down(string &s)
{
int len = s.length();
for (reg int i = ; i < len ; i ++)
if (s[i] >= 'A' and s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
} inline void add(string &s)
{
int len = s.length();
down(s);
ll cnt = ;
for (reg int i = ; i < len ; i ++)
if (s[i] == 'r') cnt++;
if (mp.find(s) == mp.end()) {
mp[s] = ++tot;
memc[tot] = mkp(cnt, len);
}
} int main()
{
n = read();
for (reg int i = ; i <= n ; i ++)
{
string s;
cin >> s;
add(s);
cp[i] = mp[s];
}
m = read();
for (reg int i = ; i <= m ; i ++)
{
string s1, s2;
cin >> s1 >> s2;
add(s1), add(s2);
ve[mp[s2]].push_back(mp[s1]);
}
queue <int> q;
for (reg int i = ; i <= tot ; i ++) q.push(i);
while (!q.empty())
{
int x = q.front();q.pop();
for (reg int i = ; i < ve[x].size() ; i ++)
{
int to = ve[x][i];
if (memc[to] > memc[x]) {
memc[to] = memc[x];
q.push(to);
}
}
}
for (reg int i = ; i <= n ; i ++)
ans1 += memc[cp[i]].first, ans2 += memc[cp[i]].second;
printf("%lld %lld\n", ans1, ans2);
return ;
}


[CF467D] Fedor and Essay的更多相关文章

  1. CF467D Fedor and Essay 建图DFS

      Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...

  2. Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  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. Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS

    题意:给一篇文章,再给一些单词替换关系a b,表示单词a可被b替换,可多次替换,问最后把这篇文章替换后(或不替换)能达到的最小的'r'的个数是多少,如果'r'的个数相等,那么尽量是文章最短. 解法:易 ...

  5. 【Codeforces 467D】Fedor and Essay

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

  6. Codeforces 467D Fedor and Essay bfs

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

  7. CF467 AB 水题

    Codeforces Round #267 (Div. 2) (C和D的题解单独写:CF467C George and Job (DP) CF467D Fedor and Essay 建图DFS) C ...

  8. cf467D(map,vector,bfs,特点提取)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. kubernetes部署高可用Harbor

    前言 本文Harbor高可用依照Harbor官网部署,主要思路如下,大家可以根据具体情况选择搭建. 部署Postgresql高可用集群.(本文选用Stolon进行管理,请查看文章<kuberne ...

  2. GlusterFs卷类型分析及创建、使用(结合kubernetes集群分析)

    引言 本文通过对卷类型的分析对比,来帮助读者选取生产环境最符合服务的挂载存储,命令可结合<glusterfs详解及kubernetes 搭建heketi-glusterfs>进行实验,下面 ...

  3. Winform中怎样根据Name获取同窗体的控件

    场景 在同一个Winform窗体中,点击一个Button按钮时, 获取同窗体的其他控件的属性. 首先需要对要获取的控件赋予Name属性,然后就可以通过Name进行获取. 实现 在Button的点击事件 ...

  4. centos7 下安装mysql5.7 数据库并使用nevicat连接数据库

    安装mysql5.7的教程: https://www.cnblogs.com/yybrhr/p/9810375.html 遇到的问题: 无法连接,到阿里云服务器安全组设置3306端口

  5. Flink 编程接口

    欢迎来 kk大数据,今天分享的是 Flink 提供了哪些编程接口可以给我们开发. 一.数据集类型 现实世界中,所有的数据都是以流式的形态产生的,不管是哪里产生的数据,在产生的过程中都是一条条地生成,最 ...

  6. [CUDA] 00 - GPU Driver Installation & Concurrency Programming

    前言 对,这是一个高大上的技术,终于要做老崔当年做过的事情了,生活很传奇. 一.主流 GPU 编程接口 1. CUDA 是英伟达公司推出的,专门针对 N 卡进行 GPU 编程的接口.文档资料很齐全,几 ...

  7. 新手学习FFmpeg - 调用API编写实现多次淡入淡出效果的滤镜

    前面几篇文章聊了聊FFmpeg的基础知识,我也是接触FFmpeg不久,除了时间处理之外,很多高深(滤镜)操作都没接触到.在学习时间处理的时候,都是通过在ffmpeg目前提供的avfilter基础上面修 ...

  8. Windows上部署MySql

    下载安装包 最新版本可以在 MySQL 下载中下载. 下载完后,我们将 zip 包解压到相应的目录,这里我将解压后的文件夹放在 D:\Program Files\Java\mysql-8.0.16-w ...

  9. Java面向对象笔记(五分钟秒懂)

    面向对象概念 面向对象三大特征:封装,继承,多态 面向对象编程(OOP,Object Oriented Programing)是相对于面向过程编程说的,之前写的代码基本都是纯的面向过程编程的,当项目复 ...

  10. C# 时间戳的相关操作

    一般的 时间戳 格式分为两种 即 10位(秒)时间戳 与 13位(毫秒)时间戳 时间戳 类型也分为两种 即 本地时间戳 与 世界统一(UTC)时间戳 废话不多说,直接上代码: 一.时间戳获取方法 // ...