CF -62B - Tyndex.Brome(二分查找)

ZOJ 1004 Anagrams by Stack(dfs堆栈) Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TR 阅读详情

B - Tyndex.Brome
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Tyndex is again well ahead of the rivals! The reaction to the release of Zoozle Chrome browser was the release of a new browser Tyndex.Brome!

The popularity of the new browser is growing daily. And the secret is not even the Tyndex.Bar installed (the Tyndex.Bar automatically fills the glass with the finest 1664 cognac after you buy Tyndex.Bottles and insert in into a USB port). It is highly popular due to the well-thought interaction with the user.

Let us take, for example, the system of automatic address correction. Have you entered codehorses instead of codeforces? The gloomy Zoozle Chrome will sadly say that the address does not exist. Tyndex.Brome at the same time will automatically find the closest address and sent you there. That's brilliant!

How does this splendid function work? That's simple! For each potential address a function of the F error is calculated by the following rules:

  • for every letter ci from the potential address c the closest position j of the letter ci in the address (s) entered by the user is found. The absolute difference |i - j| of these positions is added to F. So for every i (1 ≤ i ≤ |c|) the position j is chosen such, that ci = sj, and |i - j| is minimal possible.
  • if no such letter ci exists in the address entered by the user, then the length of the potential address |c| is added to F.

After the values of the error function have been calculated for all the potential addresses the most suitable one is found.

To understand the special features of the above described method better, it is recommended to realize the algorithm of calculating the Ffunction for an address given by the user and some set of potential addresses. Good luck!

Input

The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 105). They are the number of potential addresses and the length of the address entered by the user. The next line contains k lowercase Latin letters. They are the address entered by the user (s). Each next i-th (1 ≤ i ≤ n) line contains a non-empty sequence of lowercase Latin letters. They are the potential address. It is guaranteed that the total length of all the lines does not exceed 2· 105.

Output

On each n line of the output file print a single number: the value of the error function when the current potential address is chosen.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Sample Input

Input
2 10
codeforces
codeforces
codehorses
Output
0
12
Input
9 9
vkontakte
vcontacte
vkontrakte
vkollapse
vkrokodile
vtopke
vkapuste
vpechke
vk
vcodeforcese
Output
18
14
36
47
14
29
30
0
84

思路:把字符串和id捆绑一起,然后进行SORT,二分查找,效率挺不错的。



#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mm=2e6+9;
class node
{
  public:char s;int id;
}f[mm];
bool vis[777];
long long sum;
int n,m;
char s[mm],ss[mm];
bool cmp(node a,node b)
{
  if(a.s!=b.s)
    return a.s<b.s;
  else return a.id<b.id;
}
inline int aabs(int x)
{
  return x<0?-x:x;
}
int blook(char z,int num)
{
  int l=0,r=m-1,mid;
  int ret=999999;
  while(l<=r)
  {
    mid=(l+r)/2;
    if(f[mid].s>z)r=mid-1;
    else if(f[mid].s<z)l=mid+1;
    else
    { int zz=aabs(num-f[mid].id);
      if(ret>zz)ret=zz;
      if(f[mid].id>num)r=mid-1;
      else l=mid+1;
    }
  }
  return ret;
}
int main()
{
  while(scanf("%d%d",&n,&m)!=EOF)
  { memset(vis,0,sizeof(vis));
    scanf("%s",s);
    for(int i=0;s[i];++i)
     {
       vis[s[i]]=1;
       f[i].s=s[i];f[i].id=i;
     }
     sort(f,f+m,cmp);
    for(int i=0;i<n;++i)
    { sum=0;
      scanf("%s",ss);
      long long len=strlen(ss);
      for(int j=0;ss[j];++j)
      if(vis[ss[j]])
        sum+=blook(ss[j],j);
      else sum+=len;
      cout<<sum<<"\n";
    }
  }
}


CrewAI 多 Agent 实战包 v1(Mock免Key跑通+通义千问可选+流水线样例) 本资源是 CrewAI 风格多 Agent 可运行工程包:研究→写作→审校等流水线样例。无 API Key 时自动降级 Mock 引擎跑通;有 DASHSCOPE_API_KEY 时可接真实 CrewAI + 通义千问。含中文 README、samples、smoke 留档、单元测试。本包不含密钥。关联博客:https://blog.csdn.net/mtl1994 立即下载

相关推荐

市场销售总监年终汇报必备PPT.pptx

市场销售总监年终汇报必备PPT

CF62B Tyndex.Brome 题解

给定一个字符串作为标准字符串,然后每个字符串都与这个对比。每个字符串的每个字母的位置和标准中这个字母的位置相减取绝对值,最小的结果加到ans中,如果没有,把这个字符串的长度加到ans中。

gly2023的博客 449

农经权二轮延包—地块示意图生成

本文介绍了地块示意图制作软件的使用方法及模板制作要点。软件通过设置六个参数(示意图模板、属性数据、地块DBF、导航图、地块图片及存放位置)自动生成Word文档,并可导出PDF。文章详细说明了5页Word模板的制作过程,包括页脚三栏设置和页码插入技巧,支持每户最多28个地块的情况。同时指出该软件生成的图片分辨率(300dpi)满足清晰度要求,并提及软件免费提供插件但收费提供后续服务。

weixin_47364423的博客 1114

codeforce 62b B. Tyndex.Brome 二分查找 stl lower_bound

B. Tyndex.Brome time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Tyndex is again well ahead of the rivals! The reaction to the release of Zoozl...

caowenbo2311694605的博客 342

数控弯管机xyz转换ybc的算法_花了一年时间开发的三维弯管机交互式转档软件(三维管子模型UG,SOLIDWORK,PRO/E文件转成YBC)...

在弯管机加工中,由管子模型生成可直接进行弯管加工的YBC数据可以大大提高弯管编程过程.传统的做法是先用dxf数据文件(用autocad绘制管子的轴心线数据)转出XYZ数据,然后由XYZ数据转成YBC数据,而且大部分厂家对文件版本以及软件做管方式都有不少限制,这大大花费了用户的时间跟精力.随着企业的信息化管理以及不断的技术升级,传统用autocad线条模式来描述管子的方式正在被用户用PRO/E,UG...

weixin_29065959的博客 1961

ADS 使用调整器动态调参教程(Tuning)

ADS动态调参教程

weixin_42845306的博客 1万+

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1922

达梦数据库集群部署(主备)

达梦数据库集群部署(主备)

m0_71845127的博客 1461

CF-29C - Mail Stamps(DFS+离散化)

C - Mail Stamps Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 29C Description

nealgavin的专栏 1707

通达信-申万宏源金融终端本地数据转sqlite3,2、解析申万宏源C:/zd_swhy_gm/vipdoc/sh/lday的sh600000股票数据

为量化做准备,通达信-申万宏源金融终端本地数据转sqlite3,解析申万宏源C:/zd_swhy_gm/vipdoc/sh/lday的sh600000股票数据

wuyaqi的博客 509

Carsim2019与Simulink (Matlab2018b)联合仿真成功(超详细)

Carsim与Simulink联合仿真成功

强强的博客 3万+

uva 11624 - Fire!(bfs,3级)

Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given J

nealgavin的专栏 1351

37、半导体硅材料中的缺陷与吸杂技术解析

本文系统解析了半导体硅材料中的缺陷形成机制及其对电子器件性能的影响,重点探讨了生长和工艺诱导缺陷、表面与内部微缺陷的起源与特征。文章详细阐述了吸杂技术的原理、过程及在集成电路和太阳能电池制造中的应用,强调通过氧行为调控和热处理优化实现缺陷控制。同时介绍了吸杂效果的评估方法,并展望了未来新型吸杂技术的发展趋势,为提升半导体器件性能和制造良率提供理论支持和技术路径。

mongodb5scout的博客 162

hdu 1372 Knight Moves(广搜)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3001 Accepted Submission(s): 1868 Problem Description A friend of you is doing research on

nealgavin的专栏 1253

【WRFDA实操第一期】服务器中安装 WRFPLUS 和 WRFDA

【WRFDA实操第一期】安装 WRFPLUS 和 WRFDA

WW、forever的博客 2037

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

ZOJ 1011 NTA(二叉树+vector)

NTA Time Limit: 2 Seconds      Memory Limit: 65536 KB The NTA (Non-deterministic Tree Automata) is a kind of tree structure device. The device is built in a set of operating rules. With these

nealgavin的专栏 1176

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

CF-63E - Sweets Game(记忆化搜索dp+状态压缩)

E - Sweets Game Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 63E Description Karlsson has visited Lillebror again.

nealgavin的专栏 1082

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

CF-48E. Ivan the Fool VS Gorynych the Dragon(bfs+dfs+判重回路)

E. Ivan the Fool VS Gorynych the Dragon time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Once upon a time in

nealgavin的专栏 992

10047 - The Monocycle(bfs,4级)

Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as

nealgavin的专栏 920

hdu 4430 Yukari's Birthday(二分+枚举)

Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 151 Problem Description Today is

nealgavin的专栏 855

USACO section 2.4 Overfencing(dfs+bfs)

Overfencing Kolstad and Schrijvers Farmer John went crazy and created a huge maze of fences out in a field. Happily, he left out two fence segments on the edges, and thus created two "exits" for th

nealgavin的专栏 854

ZOJ 2913 Bus Pass(BFS+滚动队列)

Bus Pass Time Limit: 5 Seconds Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be a

nealgavin的专栏 816

USACO section 4.1 Fence Rails(搜索+优化)

Fence Rails Burch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but h

nealgavin的专栏 796

CF-25D - Roads not only in Berland(并查集或者搜索)

D - Roads not only in Berland Crawling in process...Crawling failedTime Limit:2000MSMemory Limit:262144KB64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 25D Description B

nealgavin的专栏 775
上一篇: USACO section 4.1 Beef McNuggets(数论+背包)
下一篇: CF-62D - Wormhouse(欧拉回路)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值