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

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

题意: 给一篇文章的单词,再给m对同义词(单向同义),文章中的单词可以被同义词替换,题目求经过替换后,文章中r字母出现的次数最少是多少次,如果有多个方案,要求长度最短,输出次数和长度

思路: 把每个单词视为一个点,把所有点都按照r从小到大r相同的sz从小到大的顺序排序,然后每一对同义词就是一条有向边。把单词排序后,遍历单词dfs一遍,对某个单词u,把u的所有后代的r和sz都更新为u的r和sz,最后再更新一遍文章的就ok了。注意sz需要用long long
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
const int N = ; char s[]; struct _node{
int id;
int rnum,sz;
int head;
friend bool operator < (const _node &a, const _node &b)
{
return a.rnum<b.rnum || (a.rnum==b.rnum && a.sz<b.sz);
}
};
struct _edge{
int to,next;
};
map<string,int> mp;
int id[N];
_node mes[N];
_edge edge[N];
int ecnt,mescnt;
int n,m;
bool vis[N]; int eassyid[N]; void dfs(int u,int r,int s)
{
if(vis[u]) return;
vis[u]=;
mes[u].rnum=r;
mes[u].sz=s;
for(int e=mes[u].head;e!=-;e=edge[e].next)
dfs(id[edge[e].to],r,s);
} inline void addedge(int u,int v)
{
edge[ecnt].to = v;
edge[ecnt].next = mes[u].head;
mes[u].head = ecnt++;
} inline void all_lower(int &num,int &sz)
{
int i;num=;
for(i=;s[i];i++)
{
if(s[i]>='A' && s[i]<='Z')
s[i]=s[i]-'A'+'a';
if(s[i]=='r')
num++;
}
sz=i;
}
inline int get_id()
{
int num,sz;
all_lower(num,sz);
string str(s);
if(mp.find(str)==mp.end())
{
mes[mescnt].head=-;
mes[mescnt].sz=sz;
mes[mescnt].rnum=num;
mes[mescnt].id=mescnt;
mp[str]=mescnt++;
}
return mp[str];
} int main()
{
#ifdef LOCAL
freopen("in","r",stdin);
#endif
int i;
ecnt=mescnt=;
mp.clear();
scanf("%d",&m);
for(i=;i<m;i++)
{
scanf("%s",s);
eassyid[i]=get_id();
}
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",s);
int u = get_id();
scanf("%s",s);
int v = get_id();
addedge(v,u);
}
// for(i=0;i<mescnt;i++)
// cout<<mes[i].rnum<<' '<<mes[i].sz<<endl;
sort(mes,mes+mescnt);
for(i=;i<mescnt;i++)
id[mes[i].id]=i;
memset(vis,,sizeof(vis));
for(i=;i<mescnt;i++)
if(!vis[i])
dfs(i,mes[i].rnum,mes[i].sz);
ll ansr=,ansz=;
for(i=;i<m;i++)
ansr+=mes[id[eassyid[i]]].rnum,
ansz+=mes[id[eassyid[i]]].sz;
cout<<ansr<<' '<<ansz<<endl;
return ;
}

CodeForces 467D(267Div2-D)Fedor and Essay (排序+dfs)的更多相关文章

  1. Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS

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

  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. CF467D Fedor and Essay 建图DFS

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

  4. Codeforces 467D

    题目链接 D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  6. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  7. 【Codeforces 467D】Fedor and Essay

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

  8. Codeforces 467D Fedor and Essay bfs

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

  9. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

随机推荐

  1. iOS判断为空或者只为空格

    本文转载至 :http://www.cnblogs.com/superhappy/archive/2012/11/08/2761403.html 经常有需求 要判断不能为空,后台老是鄙视不做非空判断的 ...

  2. 常见 WEB 安全漏洞(转)

    SQL注入 成因:程序未对用户的输入的内容进行过滤,从而直接代入数据库查询,所以导致了sql 注入 漏洞 . 思路:在URL处可以通过 单引号 和 and 1=1 and 1=2 等语句进行手工测试s ...

  3. MarkdownPad - The Markdown Editor for Windows http://markdownpad.com/

    MarkdownPad - The Markdown Editor for Windows http://markdownpad.com/

  4. mysql系列之8.mysql高可用 (keepalived)

    环境: centos6.5_x64 准备: 两台mysql机器 主1 master:  192.168.32.130 主2 backup:  192.168.32.131 VIP: 192.168.3 ...

  5. 【题解】 P5022旅行

    [题解]P5022 旅行 当给定你一颗树的时候,这题就是一道送分题,凉心啊! 但是给定你一颗基环树呢? 暴力断环直接跑. 但是数据范围\(n\le 1000\) 乱做就完事了. 考场上这样想的,对于\ ...

  6. 【linux】新添加一块硬盘制作LVM卷并进行分区挂载

    linux服务器新添加一块硬盘,可以直接将盘格式化挂载就能用,比如挂载在/usr/local目录,但是这样有一个弊端,就是如果这一块磁盘满了,后续想要扩容的话,不能继续挂载这个/usr/local挂载 ...

  7. yum安装软件出错解决方法

    造成yum下载安装时语法出错, 一般是由于python多个版本共存的原因.所以,只需将yum 设置文件固定python 版本,也就是python2 下面的操作能解决版本冲突问题. 1.sudo vim ...

  8. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

  9. Spring Boot2.0之整合Mybatis

    我在写这个教程时候,踩了个坑,一下子折腾到了凌晨两点半. 坑: Spring Boot对于Mysql8.1的驱动支持不好啊 我本地安装的是Mysql8.1版本,在开发时候.pom提示不需要输入驱动版本 ...

  10. 如何在Mac的Finder中显示/usr、/tmp、/var等隐藏目录

    原文链接: http://blog.csdn.net/yhawaii/article/details/7435918 Finder中默认是不显示/usr./tmp./var等隐藏目录的,通过在终端中输 ...