博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF467D Fedor and Essay 建图DFS
阅读量:6713 次
发布时间:2019-06-25

本文共 5049 字,大约阅读时间需要 16 分钟。

 

CF#267D

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……

代码:

1 //#pragma comment(linker, "/STACK:102400000,102400000")  2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std; 13 #define ll long long 14 #define usll unsigned ll 15 #define mz(array) memset(array, 0, sizeof(array)) 16 #define mf1(array) memset(array, -1, sizeof(array)) 17 #define minf(array) memset(array, 0x3f, sizeof(array)) 18 #define REP(i,n) for(i=0;i<(n);i++) 19 #define FOR(i,x,n) for(i=(x);i<=(n);i++) 20 #define RD(x) scanf("%d",&x) 21 #define RD2(x,y) scanf("%d%d",&x,&y) 22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) 23 #define WN(x) printf("%d\n",x); 24 #define RE freopen("D.in","r",stdin) 25 #define WE freopen("1biao.out","w",stdout) 26 #define mp make_pair 27 #define pb push_back 28 const double eps=1e-10; 29 const double pi=acos(-1.0); 30 31 const int maxn=222222; 32 const int maxm=maxn; 33 34 struct Edge { 35 int v,next; 36 } e[maxm]; 37 int en=0; 38 int head[maxn]; 39 40 void add(int x,int y) { 41 e[en].v=y; 42 e[en].next=head[x]; 43 head[x]=en++; 44 } 45 46 struct Node { 47 int r,len,no; 48 }; 49 bool operator<(const Node &x,const Node &y) { 50 if(x.r
y.r)return 0; 52 return x.len
S; 67 Node a[maxn]; 68 int cnt=0; 69 int main() { 70 int i,j,k; 71 string x[2]; 72 mf1(head); 73 en=0; 74 cin>>m; 75 REP(i,m)cin>>s[i]; 76 cin>>n; 77 REP(i,n) { 78 cin>>x[0]>>x[1]; 79 REP(j,2) { 80 transform(x[j].begin(), x[j].end(), x[j].begin(), ::tolower); 81 if(S[x[j]]==0) { 82 S[x[j]]=++cnt; 83 int t=0; 84 int size=x[j].size(); 85 REP(k,size)if(x[j][k]=='r')t++; 86 a[cnt].r=t; 87 a[cnt].len=size; 88 a[cnt].no=cnt; 89 } 90 } 91 92 add(S[x[1]],S[x[0]]); 93 } 94 sort(a+1,a+cnt+1); 95 mz(to); 96 FOR(i,1,cnt){ 97 if(to[a[i].no])continue; 98 aim=i; 99 dfs(a[i].no);100 }101 ll ans1=0,ans2=0;102 REP(i,m){103 transform(s[i].begin(), s[i].end(), s[i].begin(), ::tolower);104 int x=S[s[i]];105 if(x==0 || to[x]==0){106 int t=0;107 int size=s[i].length();108 REP(k,size)if(s[i][k]=='r')t++;109 ans1+=t;110 ans2+=size;111 }else{112 ans1+=a[to[x]].r;113 ans2+=a[to[x]].len;114 }115 //cout<
<
View Code

 

转载于:https://www.cnblogs.com/yuiffy/p/3980627.html

你可能感兴趣的文章
数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 执行
查看>>
单元测试(三)JUnit 进阶功能:Suites 打包测试、Categories 分类测试
查看>>
Java获取指定日期前一月(年)或后一月(年)
查看>>
实习三
查看>>
飞信系统4月29日升级后飞信机器人无法使用的解决办法
查看>>
Linux Epoll介绍和程序实例
查看>>
vue不通过路由直接获取地址栏参数的方法
查看>>
Android 唯一识别码
查看>>
Canonical今天宣布推出Plex Media Server作为Snap Store中的Snap应用程序
查看>>
gdb 学习1
查看>>
SVG TEXT 水平和垂直方向居中
查看>>
Kurento API 参考
查看>>
hello world
查看>>
C语言基础及指针⑦结构体与指针
查看>>
四种常用线程池
查看>>
兼容IE的radio遍历
查看>>
Ossim下RRDTool实战
查看>>
向服务器请求XML数据时中文乱码
查看>>
微信消息接口发送信息到分组和用户,错误代码40003和40008
查看>>
HTTP状态码 错误列表
查看>>