CodeForces 467D(267Div2-D)Fedor and Essay (排序+dfs)
2 seconds
256 megabytes
standard input
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.
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.
Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.
3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
2 6
2
RuruRu fedya
1
ruruRU fedor
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)的更多相关文章
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
题意:给一篇文章,再给一些单词替换关系a b,表示单词a可被b替换,可多次替换,问最后把这篇文章替换后(或不替换)能达到的最小的'r'的个数是多少,如果'r'的个数相等,那么尽量是文章最短. 解法:易 ...
- 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 ...
- CF467D Fedor and Essay 建图DFS
Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...
- Codeforces 467D
题目链接 D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- 【Codeforces 467D】Fedor and Essay
Codeforces 467 D 题意:给\(m\)个单词,以及\(n\)个置换关系,问将\(m\)个单词替换多次后其中所含的最少的\(R\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...
- Codeforces 467D Fedor and Essay bfs
题目链接: 题意: 给定n个单词. 以下有m个替换方式.左边的单词能变成右边的单词. 替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短 输出字母r的个数和单词长度. 思路: 我们觉得 ...
- 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 ...
随机推荐
- 关于System.Data.ParameterDirection四个枚举类型所起的作用(转)
相信大家都知道.net中有四个关于参数传入传出的类型 分别是: System.Data.ParameterDirection.Input System.Data.ParameterDirection. ...
- runsv
runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...
- 使用AXIS2作为Client訪问WebService
使用AXIS2,能够方便的构建WebService的server端,也能够非常方便的作为Cilent,来訪问别的WebService. 以下依据工作中的经历,整理了一下,作为Cilent訪问WebSe ...
- 打开蓝牙debug hci log
Android4.2之前抓取hci log都是通过hcidump命令完成的,但是Android4.2 Bluetooth引入了Bluedroid,这是一个新的蓝牙协议栈.所以抓取hci log的方法也 ...
- 同源策略 , CORS
一 . 同源策略 同源策略( Same origin policy ) 是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响,可以说Web是构建在同源 ...
- tensorflow学习官网地址
摘自: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/overview.html 内容很多,需要花时间看完
- 世界各国Google网址大全
http://www.oschina.net/question/100896_50293 冰岛 https://www.google.is/ 丹麦 https://www.google.dk/ 挪威 ...
- java高级特性增强
第4天 java高级特性增强 今天内容安排: 1.掌握多线程 2.掌握并发包下的队列 3.了解JMS 4.掌握JVM技术 5.掌握反射和动态代理 java多线程增强 .1. java多线程基本知识 . ...
- CI框架上传csv文件
今天遇到在用CI框架上传csv文件时报错问题: The filetype you are attempting to upload is not allowed. 是类型不允许,想到CI框架的conf ...
- 分享知识-快乐自己:搭建第一个 Hibernate (Demo)
使用 Hibernate 完成持久化操作 七大 步骤: 1.读取并解析配置文件及映射文件: Configuration configuration=new Configuration().config ...