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. Linux 开启端口,操作防火墙

    命令行方式:    添加10002端口方法 1.vi etc/sysconfig/iptables 2. 开放端口命令:-A INPUT -p tcp -m state --state NEW -m ...

  2. Objective-C 内存管理之dealloc方法中变量释放处理

    本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101ds8j.html   (一).关于nil http://cocoadevcentral.com/d/ ...

  3. 多线程快速解压FastZipArchive介绍

    本文转载至  http://blog.csdn.net/xunyn/article/details/12975937   多线程解压iosfast 在iOS项目中用到解压缩,用的是ZipArchive ...

  4. 怎么利用jquery.form 提交form

    说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...

  5. JavaScript library of crypto standards. 看源码

    crypto-js - npm https://www.npmjs.com/package/crypto-js crypto-js/docs/QuickStartGuide.wiki <wiki ...

  6. 【题解】Coins(二进制拆分+bitset)

    [题解]Coins(二进制拆分+bitset) [vj] 俗话说得好,bitset大法吼啊 这道题要不是他多组数据卡死了我复杂度算出来等于九千多万的选手我还不会想这种好办法233 考虑转移的实质是怎样 ...

  7. 我的Java开发学习之旅------>计算从1到N中1的出现次数的效率优化问题

    有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数.比如f(1)=1:f(13)=6,问一个最大的能满足f(n)=n中的n是什么? 例如:f(13)=6, 因为1, ...

  8. mysql一:操作数据库

    创建数据库是指在数据库空间中划出一块空间用来存储相关的数据,表就是存储在对应的数据库里面.首先来看下查看数据库的命令:show databases. 这个是用来查询数据库空间下所有的数据库,其中inf ...

  9. opencv轮廓提取、轮廓识别相关要点

    1.轮廓提取 src = cv2.imread("***.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src ,cv2.COLOR_BG ...

  10. Java for LeetCode 137 Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...