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. Fragment 懒加载

    我们在遇到Activity嵌套Fragment的时候经常会遇到多个Fragment页面同时加载数据,一开始的时候就初始化很多页面,有的甚至进入页面的时候,会造成缓慢的现象,下面我们就针对这个问题做一下 ...

  2. tp框架知识 之(链接数据库和操作数据内容)

    框架有时会用到数据库的内容,在"ThinkPhp框架知识"的那篇随笔中提到过,现在这篇随笔详细的描述下. 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写con ...

  3. TP框架---thinkphp查询和添加数据

    查询 <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controlle ...

  4. 【BZOJ4383】[POI2015]Pustynia 线段树优化建图

    [BZOJ4383][POI2015]Pustynia Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r ...

  5. nagios-plugins安装报错--with-mysql: no

    --with-mysql: no 解决方法 yum安装mysql-devel yum install mysql-devel

  6. 改善程序与设计的55个具体做法 day1

    博客好久没更新了,就从这本读书笔记开始吧. 条款01: 视C++为一个语言联邦 C++可视为有四个次语言组成的: 1.C语言 2.Object-Oriented C++ (面向对象C++) 3.Tem ...

  7. 【Leetcode-easy】Remove Duplicates from Sorted Array

    题目要求:删除排好序的含有重复元素的数组.返回去除重复后的数组长度,并更新原始数组.不能使用额外空间. 思路:要不额外的使用内存空间,那么只有遍历数组,count保持下一个不重复的数字,遍历过程中如果 ...

  8. C# nunit 单元测试

    1. 引包 nunit.framework.dll

  9. 分享知识-快乐自己:Oracle 创建序列 及 使用序列

    1.创建序列语法: create sequence 序列名 [可选参数] 序列名常定义为‘seq_XXX’的形式,创建序列不能使用replace 可选参数说明: increment by: 序列每次增 ...

  10. zoj 3813 Alternating Sum(2014ACMICPC Regional 牡丹江站网络赛 E)

    1.http://blog.csdn.net/dyx404514/article/details/39122743 思路:题目意思很清楚了,这里只说思路. 设区间[L,R],区间长度为len=(R-L ...