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.

Examples
input

Copy
3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
output
2 6
input

Copy
2
RuruRu fedya
1
ruruRU fedor
output
1 10
题意:给你一段有n个单词的句子,再给出m个可以以左代替右的转换(单向的),求整个句子最少有几个r和最少的字母。
分析:从题意可知我们可以用bfs遍历找出每个单词最优的情况,怎样找出最优的情况呢,我们由题意可知r和整句单词数要最少
我们就可以从每个单词中提取出r的数量cnt和每个单词的长度len。然后利用map函数中的count找出是否有一样的单词,用v[i][j]数组
记录第i个单词的可以代替第v[i][j]个单词,v[i].size()可以快捷的找出有多少个单词可由第i个单词代替,具体看代码解析。
代码:

#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#include<queue>
#define Max 100010
using namespace std;
typedef __int64 ll;
map<string,int> mp;//记录不同单词的数量
vector<int> v[Max*2];//记录第j可以换成i
int n,m,w[Max];//记录要处理的单词
int sz;
struct
{
  int len;
  int cnt;
}st[Max*3],st1;//记录每个单词的长度和r的数量
int dt(string& s)
{
  int len=s.length();
  int i;
  int cnt=0;
  for(i=0;i<len;i++)//全转换为小写字母以便处理
  {
    if(s[i]<'a')
    s[i]+='a'-'A';
    if(s[i]=='r')
    cnt++;//记录r数
  }
  if(!mp.count(s))//加入不同的单词,mp.count(s)可以查找mp中是否有和s一样的单词
  {
    //printf("sada\n");
    mp[s]=sz;//给每个单词标个号
    st[sz].cnt=cnt;
    //printf("%d %d %d\n",sz,cnt,len);
    st[sz++].len=len;
  }
return 0;
}
int bfs()
{
  queue<int> q;
  int i,j;
  for(i=0;i<sz;i++)//放入所有的单词,以便将其全部化为最优情况
  q.push(i);
  //printf("sz=%d\n",sz);
  while(!q.empty())
  {
    int a=q.front();
    q.pop();
    st1=st[a];//提取出当前编号单词的特性
    int len=v[a].size();//有多少单词可以由当前单词代替
    for(i=0;i<len;i++)
    {
      int x1=v[a][i];
      //printf("v%d",x1);
      if(st1.cnt<st[x1].cnt)//判断r的数量
      {
        st[x1].cnt=st1.cnt;
        st[x1].len=st1.len;
        q.push(x1);
      }
      else if(st1.cnt==st[x1].cnt&&st1.len<st[x1].len)//判断长度
      {
        st[x1].len=st1.len;
        q.push(x1);
      }
    }
    //printf("a%d %d\n",a,q.empty());
  }
  return 0;
}
string s1,s2;
int main()
{
  cin>>n;
  int i,j;
  for(i=0;i<n;i++)
  {
    getchar();
    cin>>s1;
    dt(s1);
    w[i]=mp[s1];//记录要处理的单词
  }
  cin>>m;
  for(i=0;i<m;i++)
  {
    cin>>s1>>s2;
    dt(s1);
    dt(s2);
    v[mp[s2]].push_back(mp[s1]);//注意:单词的转换是由左到右,单向的;
  }
  bfs();
  ll cnt=0,len=0;//小心cnt与len超过2^32(每个单词<=100000,每条语句小于100000个单词
  for(i=0;i<n;i++)//因为前面的处理已将所有单词转换为最优情况了
  {
    cnt+=st[w[i]].cnt;
    len+=st[w[i]].len;
  }
  cout<<cnt<<" "<<len;
  return 0;
}

cf467D(map,vector,bfs,特点提取)的更多相关文章

  1. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

  2. uva--11991 - Easy Problem from Rujia Liu?(sort+二分 map+vector vector)

    11991 - Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for e ...

  3. map,vector 等容器内容的循环删除问题(C++)

    map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...

  4. 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)

    传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...

  5. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  6. 几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table

    list支持快速的插入和删除,但是查找费时; vector支持快速的查找,但是插入费时. map查找的时间复杂度是对数的,这几乎是最快的,hash也是对数的.  如果我自己写,我也会用二叉检索树,它在 ...

  7. Set,List,Map,Vector,ArrayList的区别(转)

    JAVA的容器---List,Map,Set Collection ├List │├LinkedList │├ArrayList │└Vector │ └Stack └Set Map ├Hashtab ...

  8. ACM: NBUT 1646 Internet of Lights and Switches - 二进制+map+vector

    NBUT 1646 Internet of Lights and Switches Time Limit:5000MS     Memory Limit:65535KB     64bit IO Fo ...

  9. c++如何遍历删除map/vector里面的元素

    新技能Get! 问题 对于c++里面的容器, 我们可以使用iterator进行方便的遍历. 但是当我们通过iterator对vector/map等进行修改时, 我们就要小心了, 因为操作往往会导致it ...

随机推荐

  1. vue select的change事件,将点击过的城市名存在数组中,下次调用不需要再调用接口

    <template> <div id="body" class="application" v-show="show" v ...

  2. react中创建组件

    第1种 - 创建组件的方式 > 使用构造函数来创建组件,如果要接收外界传递的数据,需要在 构造函数的参数列表中使用`props`来接收:> 必须要向外return一个合法的JSX创建的虚拟 ...

  3. World Tour CodeForces - 667D (bfs最短路)

    大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大

  4. ie8不支持currentTarget的解决办法

    一般绑定事件时,我们都会在事件回调方法里用event.currentTarget获取当前对象,但到ie8里就获取不到了. 解决方法如下: var eve = event || window.event ...

  5. mybatis*中DefaultVFS的logger乱码问题

    从网上下的Java Persistence with MyBatis 3的源码 出现这个问题的原因是logback记日志的时候乱码 ResolverUtil - Not a JAR: file:... ...

  6. 数据结构与算法之PHP查找算法(二分查找)

    二分查找又称折半查找,只对有序的数组有效. 优点是比较次数少,查找速度快,平均性能好,占用系统内存较少: 缺点是要求待查表为有序表,且插入删除困难. 因此,折半查找方法适用于不经常变动而查找频繁的有序 ...

  7. Golang 在 Mac、Linux、Windows 下如何交叉编译(转)

    原文地址:Golang 在 Mac.Linux.Windows 下如何交叉编译 Golang 支持交叉编译,在一个平台上生成另一个平台的可执行程序,最近使用了一下,非常好用,这里备忘一下. Mac 下 ...

  8. 33 个 2017 年必须了解的 iOS 开源库

    本文翻译自Medium,原作者为Pawe? Bia?ecki 照片版权:(Unsplash/Markus Pe) 你好,iOS 开发者们!我的名字叫 Pawe?,我是一个独立 iOS 开发者,并且是  ...

  9. CM+CDH安装教程(CentOS)

    一.简单介绍 CM:Cloudera Manager,Cloudera公司编写的一个CDH的管理后台,类似各CMS的管理后台. CDH:Cloudera’s distribution,includin ...

  10. Linux/AIX/Windows端口和进程互查

    经常我们需要通过端口号查是哪个进程占用,或想查看某个进程占用了哪些端口. 1.1Linux上通过端口查找进程 比如我们想知道当前系统中80端口是哪个进程所占用 netstat -anp| |more ...