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. 连环清洁工之特殊任务--java资源如何关闭?

    小C是一名特殊的黑客,他专门为黑客提供服务,扫除黑客攻击的痕迹,避免被查到为何人攻击. 今天他正兴致勃勃的玩游戏<连环清洁工>,连环清洁工是由iFun4all S.A.制作发行的一款犯罪题 ...

  2. 控制执行流程之ForEach语法

    1.定义 : 一种更加简洁的for语法,用于数组和容器===>>>foreach,无需创建int类型变量对被访问项构建的序列进行计数. 2.经典应用:  对于数组的初始化,如果选用f ...

  3. 从零开始使用 Webpack 搭建 Vue 开发环境

    创建项目 先创建一个空目录,在该目录打开命令行,执行 npm init 命令创建一个项目(无法执行 npm 命令?需要先安装 Node),这个过程会提示输入一些内容,随意输入就行,完成后会自动生成一个 ...

  4. 使用 Fabric 自动化部署 Django 项目

    作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在上一篇教程中,我们通过手工方式将代码部署到了服务器.整个过程涉及到十几条命令,输了 ...

  5. Intellij IDEA 2019 + Java Spring MVC + Hibernate学习笔记(2)

    书接上文 首先根据各种Spring MVC教程,建立了基础的结构,是否合理不知道,姑且先这样,有问题再解决问题.学习新东西,不能怕掉坑里... 查询网上别人的经历说需要把根目录下的lib目录下的所有包 ...

  6. gym102346题解

    B Buffoon 判断最大值是不是第一个数,签到题. H Hour for a Run 输出\(n*m\)的\(10\%\)到\(90\%\),签到题,注意别用浮点数和ceil,有精度问题. M M ...

  7. 用 CocosCreator 快速开发推箱子游戏

    游戏总共分为4个功能模块: - 开始游戏(menuLayer) - 关卡选择(levelLayer) - 游戏(gameLayer) - 游戏结算(gameOverLayer) Creator内组件效 ...

  8. markdown常用语法使用笔记

    markdown是当下比较流行的一种编辑标记语言,很多系统都支持markdown语法来编辑文件内容,像gitbook之类的,一下是一些学习笔记. 1.开头用#的数量表示1-6阶的标题,结尾可以以任意数 ...

  9. Redis-->windows上的安装教程

    Windows下安装Redis服务 说明:本文拷贝自http://www.cnblogs.com/jaign/articles/7920588.html Redis是有名的NoSql数据库,一般Lin ...

  10. 06-border

    边框 border:边框,描述盒子的边框 边框的三要素:粗细 线性样式 颜色 例如:border:1px solid red: 如果颜色不写,默认是黑色:如果粗细不写,不显示边框:如果只写线性样式,默 ...