cf467D(map,vector,bfs,特点提取)

本文介绍了一个算法,旨在帮助用户通过替换单词的同义词来最小化英语作文中的字母「R」数量及整体长度。该算法使用了BFS遍历方法,并结合同义词词典进行单词替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

转载于:https://www.cnblogs.com/cglongge/p/8678621.html

内容概要:本文深入解析了扣子COZE AI编程及其详细应用代码案例,旨在帮助读者理解新一代低门槛智能体开发范式。文章从五个维度展开:关键概念、核心技巧、典型应用场景、详细代码案例分析以及未来发展趋势。首先介绍了扣子COZE的核心概念,如Bot、Workflow、Plugin、Memory和Knowledge。接着分享了意图识别、函数调用链、动态Prompt、渐进式发布及监控可观测等核心技巧。然后列举了企业内部智能客服、电商导购助手、教育领域AI助教和金融行业合规质检等应用场景。最后,通过构建“会议纪要智能助手”的详细代码案例,展示了从需求描述、技术方案、Workflow节点拆解到调试与上线的全过程,并展望了多智能体协作、本地私有部署、Agent2Agent协议、边缘计算插件和实时RAG等未来发展方向。; 适合人群:对AI编程感兴趣的开发者,尤其是希望快速落地AI产品的技术人员。; 使用场景及目标:①学习如何使用扣子COZE构建生产级智能体;②掌握智能体实例、自动化流程、扩展能力和知识库的使用方法;③通过实际案例理解如何实现会议纪要智能助手的功能,包括触发器设置、下载节点、LLM节点Prompt设计、Code节点处理和邮件节点配置。; 阅读建议:本文不仅提供了理论知识,还包含了详细的代码案例,建议读者结合实际业务需求进行实践,逐步掌握扣子COZE的各项功能,并关注其未来的发展趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值