Codeforces Round #267 (Div. 2)

CF#267D

D - Fedor and Essay

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个单词,表示原文章。给出由n组单词组成的同义词字典,每组单词表示左边的可以换成右边的(单向)。(大小写均不敏感)。可以对原文章的各个单词进行若干次换,最后要得到字母“r”最少。若字母r数量相同,则要求总长度最短的(总长度为各个单词的长度和)。输出最后字母r的数量和总长度。

题解:

STLmap+建图+dfs

思路是把一个词向r尽量少、r相同的话长度尽量少的词变,于是可以对字典建个图,将能到达r最少的长度最短的点x的点全部标为换为x。

利用map将字典中的词与图中的点对应,将一组词的右边词向左边词连一条边(表示右边词有一条来自左边词的入边)。

将点按要求排序(r少的在前面,r相同的话长度小的在前面)。

然后依次对排序后的点进行dfs,所经之点的to[x]全部标为这次dfs的起点aim,只经过to[x]还没确定的点。

最后对原文章进行搞,如果原文章的某个词在字典里,就把它变成to[x]指向的词;否则不能换。

注意因为替换操作,会超int……

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const double eps=1e-;
const double pi=acos(-1.0); const int maxn=;
const int maxm=maxn; struct Edge {
int v,next;
} e[maxm];
int en=;
int head[maxn]; void add(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} struct Node {
int r,len,no;
};
bool operator<(const Node &x,const Node &y) {
if(x.r<y.r)return ;
if(x.r>y.r)return ;
return x.len<y.len;
} int to[maxn];
int aim;
void dfs(int x){
to[x]=aim;
for(int i=head[x];i!=-;i=e[i].next){
if(to[e[i].v]==) dfs(e[i].v);
}
} int m,n;
string s[];
map<string,int>S;
Node a[maxn];
int cnt=;
int main() {
int i,j,k;
string x[];
mf1(head);
en=;
cin>>m;
REP(i,m)cin>>s[i];
cin>>n;
REP(i,n) {
cin>>x[]>>x[];
REP(j,) {
transform(x[j].begin(), x[j].end(), x[j].begin(), ::tolower);
if(S[x[j]]==) {
S[x[j]]=++cnt;
int t=;
int size=x[j].size();
REP(k,size)if(x[j][k]=='r')t++;
a[cnt].r=t;
a[cnt].len=size;
a[cnt].no=cnt;
}
} add(S[x[]],S[x[]]);
}
sort(a+,a+cnt+);
mz(to);
FOR(i,,cnt){
if(to[a[i].no])continue;
aim=i;
dfs(a[i].no);
}
ll ans1=,ans2=;
REP(i,m){
transform(s[i].begin(), s[i].end(), s[i].begin(), ::tolower);
int x=S[s[i]];
if(x== || to[x]==){
int t=;
int size=s[i].length();
REP(k,size)if(s[i][k]=='r')t++;
ans1+=t;
ans2+=size;
}else{
ans1+=a[to[x]].r;
ans2+=a[to[x]].len;
}
//cout<<s[i]<<endl;
//printf("%d %d %d %d %d\n",i,x,to[x],ans1,ans2);
}
printf("%I64d %I64d\n",ans1,ans2);
return ;
}

CF467D Fedor and Essay 建图DFS的更多相关文章

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

  2. [CF467D] Fedor and Essay

    After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying com ...

  3. zzulioj--1831-- 周末出游(vector建图+dfs)

    1831: 周末出游 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 22  Solved: 8 SubmitStatusWeb Board Descr ...

  4. Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)

    You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...

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

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

  6. 7月13日考试 题解(DFS序+期望+线段树优化建图)

    T1 sign 题目大意:给出一棵 N 个节点的树,求所有起点为叶节点的有向路径,其 上每一条边权值和的和.N<=10000 水题.考试的时候毒瘤出题人(学长orz)把读入顺序改了一下,于是很多 ...

  7. 【BZOJ-1570】BlueMary的旅行 分层建图 + 最大流

    1570: [JSOI2008]Blue Mary的旅行 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 388  Solved: 212[Submit ...

  8. UVa 3487 & 蜜汁建图

    题意: 有两家公司都想向政府申请某些资源的使用权,并且他们都提供了一些申请列表,列表中含有申请费用和资源种类,同一家公司的申请列表之间不含有重复的资源.政府只可以完整地接受和拒绝谋一份申请列表,问政府 ...

  9. BZOJ-1927 星际竞速 最小费用最大流+拆点+不坑建图

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Submit: 1593 Solved: 967 [Submit][Statu ...

随机推荐

  1. AutoIt3(AU3)开发的装机小工具,实现快速检测以及一些重用快捷操作功能

    项目相关地址 源码:https://github.com/easonjim/Installed_Tools bug提交:https://github.com/easonjim/Installed_To ...

  2. Python基础0:变量 赋值 表达式和运算符

    变量: 前面我们在使用print()输出内容的时候,如果内容很长,后面要再次输出的时候,就需重新在输入一遍. 如果给输出的内容起个简单的别名.这样我们用简短的别名来代替长内容,下次要输出的时候就直接使 ...

  3. dedeCMS /data/mysql_error_trace.php DB error raised PHP Code Injection Via /include/dedesql.class.php Log FIle Without Access Validation

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 dedecms采用面向对象封装的方式实现了功能操作的模块集中化,例如对于数据库管理 ...

  4. 解决Gmail/GCalendar图标丢失问题

    由于 ssl.gstatic.com被墙的原因,GFW里面访问gmail/gcalendar的时候好多图标出不来,用起来很窝火,已经有一阵子了.曾经搜 过不少解决方案,但是总是时好时坏.今天总算试验出 ...

  5. Mac下同时安装多个版本的JDK

    JDK8 GA之后,小伙伴们喜大普奔,纷纷跃跃欲试,想体验一下Java8的Lambda等新特性,可是目前Java企业级应用的主打版本还是JDK6, JDK7.因此,我需要在我的电脑上同时有JDK8,J ...

  6. python列表、元组、字典(四)

    列表 如:[11,22,33,44,44].['TangXiaoyue', 'bruce tang'] 每个列表都具备如下功能: class list(object): ""&qu ...

  7. gnuplot使用2

    设置图中连线的颜色.宽度.连线样式等 set style line 每个显示终端都有默认的线类型和点类型集合,可以通过在命令行输入: test查看,如下图显示了在wxt终端模式下默认的线的集合和点的集 ...

  8. NSXMLParser解析本地.xml数据(由于like7xiaoben写的太好了,我从她那里粘贴过来的)

    NSXMLParser解析简要说明 .是sax方法解析 .需要创建NSXMLParser实例 (alloc) 并创建解析器 (initWithData:) 为解析器定义委托 (setDelegate: ...

  9. 在Linux下安装和使用MySQL

    [简 介] 想使用Linux已经很长时间了,由于没有硬性任务一直也没有系统学习,近日由于工作需要必须使用Linux下的MySQL.本以为有Windows下使用SQL Server的经验,觉得在Linu ...

  10. python学习笔记-(六)深copy&浅copy

    在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用. 1. 赋值 赋值其实只是传递对象引用,引用对象 ...