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 ...
随机推荐
- EntityFramwork 查询
EntityFramwork 查询 1.简单查询: SQL: SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID EF: // ...
- js中scrollLeft、scrollWidth、offsetTop等相关位置属性的理解(转)
1.常见的事件位置属性 e.pageX——相对整个页面的坐标 注意:IE6.IE7.IE8无该属性 e.layerX——相对当前坐标系的border左上角开始的坐标 注意:在opera.IE6.IE7 ...
- Unix环境高级编程—进程控制(二)
一.函数wait和waitpid 今天我们继续通过昨天那个死爹死儿子的故事来讲(便于记忆),现在看看wait和waitpid函数. #include<sys/wait.h> pid_t w ...
- Machine Learning No.5: Neural networks
1. advantage: when number of features is too large, so previous algorithm is not a good way to learn ...
- windows下创建做一个类似与linux 的SFTP
在项目中经常需要做通过ftp上传文件到ftp上,如果服务器是windows版的服务器又该如何做呢,下面就给大家介绍一个软件:freeSSHd 软件地址 http://www.freesshd.c ...
- 最全面的HashMap和HashTable的区别
找工作期间不少企业都会问到有关HashMap和HashTable两者直接的区别,很多博客里虽然有提及但总是没有那么全面,只是一些常用的不同,现在就我自己所总结的比较全面的不同,归纳以下: HashMa ...
- JAVA- 清除数组重复元素
清除数组重复元素并打印新数组. import java.util.*; public class Repeat { public static void main(String[] args) { / ...
- BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...
- 利用ThinkPHP做项目步骤
ThinkPHP使用规则:约定大于配置 创建入口文件: 1.在ThinkPHP目录下创建一个入口文件index.php 2.访问入口文件的同时系统会自动把对应的应用目录文件Test创建出来 3.打开H ...
- html5--2.3新的布局元素(2)-article
html5--2.3新的布局元素(2)-article 学习要点 了解article元素的语义和用法 完成一个简单的实例 article元素(标签) 用于定义一个独立的内容区块,比如一篇文章,一篇博客 ...