【2019南昌邀请赛现场赛 - J】Prefix(STLmap,思维)

题干:

yah has n strings <s1​,⋯,sn​>, and he generates a sequence P by two steps:

P=<s1​,⋯,sn​>
Replace each si​ with all prefixes of itself.
An example is:

the n strings are < aab,ab >
first, P =< aab,ab >
then, P =< a,aa,aab,a,ab >
There are 26 positive integers d1​,⋯,d26​ that denote the difficulty to identify lowercase English letter a,b,⋯,z for yah.

The difficulty to identify a string strstr is (i=1∏∣str∣​dstri​​) mod  m

Now, yah wants to calculate: for each string si​(i=1,⋯,n), the number of strings in P that is prefix of si ​and more difficult than si​.

Input
The first line contains two integers n,mn,m(1≤n≤105,1≤m≤2×105).

The second line contains 26 positive d1​,⋯,d26​, 0^50≤di​≤105.

The following n lines, each line contains a string, the ith​ lines contains si​, the sum of length of all strings is not great than 2×105.

Output
The only line contains n integers, the ith​ integer denoting the number of strings in P that is prefix of si​ but more difficult to identify for yah.

题意:
给定n代表n个字符串,m代表模数,再给定26个字母的每个字母的权值d,再定义字符串s的值:前i个字符对应的子串的值dd[i]= dd[i-1]*(d[s[i]])  %  m 。

输入n个字符串,求对于每个字符串,大于该字符串 的 值 的 前缀的 个数(值dd[i]= dd[i-1]*(d[s[i]])  %  m ),主要求前缀的个数

解题报告:

map暴力搞一搞就能过、、但是要真是比赛场还是老老实实写hash吧、、万一T了呢。

其实也可以字典树,存前缀出现的次数。也不难想。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
map<string,PII> mp; 
int n;
int d[MAX];
ll m;
string s[MAX],tmp;
int main()
{ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);cin>>n>>m;for(int i = 0; i<26; i++) cin>>d[i];for(int i = 1; i<=n; i++) {cin>>s[i];tmp = "";int len = s[i].length();ll tmpp = 1;for(int j = 0; j<len; j++) {tmp += s[i][j];tmpp *= d[s[i][j]-'a'];tmpp %= m;auto it = mp.find(tmp);if(it == mp.end()) mp[tmp] = pm(tmpp,1);else mp[tmp].SS ++;} }for(int i = 1; i<=n; i++) {tmp = "";int ans = 0;int tar = mp[s[i]].FF;int len = s[i].length();for(int j = 0; j<len; j++) {tmp += s[i][j];if(mp[tmp].FF > tar) ans += mp[tmp].SS;}cout << ans ;if(i == n) cout << "\n";else cout << " ";//printf("%d%c",ans,i == n ?'\n' :' ');}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439811.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Apollo进阶课程㉔丨Apollo 规划技术详解——Motion Planning Environment

原文链接&#xff1a;进阶课程㉔丨Apollo 规划技术详解——Motion Planning Environment 自动驾驶汽车核心技术包括环境感知、行为决策、运动规划与控制等方面。其中&#xff0c;行为决策系统、运动规划与控制系统作为无人驾驶汽车的“大脑”&#xff0c;决定了其在不同交通驾…

JAVA类 与类文件

在一个.java文件中可以有多个同级类, 其修饰符只可以public&#xff0f;abstract&#xff0f;final&#xff0f;和无修饰符1.public修饰的只能有一个,且必须要与文件名相同: 因为jvm虚拟机为了提高查找类的速度&#xff0c;使用import语句导入的时候&#xff0c;只会导入对应空…

【2019南昌邀请赛现场赛 - G】Winner(建图,tarjan缩点 或 贪心)

题目大意&#xff1a; n个人参加竞技比赛。比赛由三种模式a,b,c&#xff0c;每个人在每种模式下有对应的权值a[i]b[i]c[i]。举行n−1场比赛&#xff0c;每场比赛主办方可以选择两个人决斗&#xff0c;能力值低的人淘汰。这样保证n-1场比赛过后&#xff0c;只会有一个winner。q…

一步步编写操作系统 26 打开A20地址线

打开A20地址线 还记得实模式下的wrap-around吗&#xff1f;也就是地址回绕。咱们一起来复习一下。实模式下内存访问是采取“段基址:段内偏移地址”的形式&#xff0c;段基址要乘以16后再加上段内偏移地址。实模式下寄存器都是16位的&#xff0c;如果段基址和段内偏移地址都为1…

【HDU - 6567】Cotree(树形dp,思维)

题干&#xff1a; Avin has two trees which are not connected. He asks you to add an edge between them to make them connected while minimizing the function ∑ni1∑nji1dis(i,j)∑i1n∑ji1ndis(i,j), where dis(i,j)dis(i,j) represents the number of edges of the …

一步步编写操作系统 27 处理器微架构之流水线简介

了解处理器内部硬件架构&#xff0c;有助于理解软件运行原理&#xff0c;因为这两者本身相辅相成&#xff0c;相互依存。就像枪和狙击手&#xff0c;枪的操作和外形设计都是要根据人体工学&#xff0c;让人不仅操作容易&#xff0c;而且携带也要轻便&#xff0c;做到能随时射出…

【2019icpc南京站网络赛 - F】Greedy Sequence(思维,贪心构造,STLset)

题干&#xff1a; Youre given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n], construct a sequence s_isi​ by the following rules: s_i[1]isi​[1]i;The length of s_isi​ is nn, and for each j \in [2, n]j∈[2,n], s_…

一步步编写操作系统 28 cpu乱序执行

乱序执行(乱序执行译作异步执行更贴切)&#xff0c;是指在cpu中运行的指令并不按照代码中的顺序执行&#xff0c;而是按照一定的策略打乱顺序执行&#xff0c;也许后面的指令先执行&#xff0c;当然&#xff0c;得保证指令之间不具备相关性。 举个简单的例子&#xff0c;比如如…

【POJ - 1741】Tree(树分治,容斥,点分治,模板题)

题干&#xff1a; Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not e…

Apollo进阶课程㉚丨Apollo ROS背景介绍

原文链接&#xff1a;进阶课程㉚丨Apollo ROS背景介绍 ROS是机器人学习和无人车学习最好Linux平台软件&#xff0c;资源丰厚。无人车的规划、控制算法通常运行在Linux系统上&#xff0c;各个模块通常使用ROS进行连接。 上周阿波君为大家详细介绍了「进阶课程㉙Apollo控制技术详…

一步步编写操作系统 29 cpu缓存简介

缓存是20世纪最大的发明&#xff0c;其原理用一些存取速度较快的存储设备做为数据缓冲区&#xff0c;避免频繁访问速度较慢的低速存储设备&#xff0c;归根结底的原因是&#xff0c;低速存储设备是整个系统的瓶颈&#xff0c;缓存用来缓解“瓶颈设备”的压力。 之前介绍实模式…

【CodeForces - 1096D】Easy Problem(dp,思维)

题目大意&#xff1a; 现在有一个由小写字母组成的字符串&#xff0c;去掉这个字符串的第i个位置的字符会有ai的代价。你的任务是去掉这个字符串中的一些字符使得该字符串中不包含子序列hard&#xff0c;且去掉字符的代价之和尽可能小。 输入 第一行一个整数n表示字符串的长…

一步步编写操作系统 30 cpu的分支预测简介

人在道路的分岔口时要预测哪条路能够到达目的地&#xff0c;面对众多选择时&#xff0c;计算机也一样要抉择&#xff0c;毕竟计算机的运行方式是以人的思路来设计的&#xff0c;计算机中的抉择其实就是人在抉择。 cpu中的指令是在流水线上执行。分支预测&#xff0c;是指当处理…

【HDU - 5492】Find a path(dp,tricks)

题干&#xff1a; Frog fell into a maze. This maze is a rectangle containing NN rows and MM columns. Each grid in this maze contains a number, which is called the magic value. Frog now stays at grid (1, 1), and he wants to go to grid (N, M). For each step,…

Apollo进阶课程㉜丨Apollo ROS原理—1

原文链接&#xff1a;进阶课程㉜丨Apollo ROS原理—1 ROS在开发过程中&#xff0c;基于功能把整个自动驾驶系统分成多个模块&#xff0c;每个模块负责自己消息的接收、处理、发布。当模块需要联调时&#xff0c;通过框架可以把各个模块快速的集成到一起。 上周阿波君为大家详细…

Ubuntu下安装Chrome浏览器的两个方法

一、通过直接下载安装Google Chrome浏览器deb包。 打开Ubuntu终端&#xff0c;以下为32位版本&#xff0c;使用下面的命令。 wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb 以下为64位版本&#xff0c;使用下面的命令。 wget https://dl.…

【EOlymp - 2908】SumThem All(数位统计,tricks)

题干&#xff1a; Find the sum of all the digits in all the integers between lowerBound and upperBound inclusive. Input Each line contains two integers lowerBound and upperBound (0 ≤ lowerBound ≤ upperBound ≤ 2109). Output For each test case print i…

Apollo进阶课程㉝丨Apollo ROS原理—2

原文链接&#xff1a;进阶课程㉝丨Apollo ROS原理—2 在ROS系统中&#xff0c;从数据的发布到订阅节点之间需要进行数据的拷贝。在数据量很大的情况下&#xff0c;很显然这会影响数据的传输效率。所以Apollo项目对于ROS第一个改造就是通过共享内存来减少数据拷贝&#xff0c;以…

Java 10 常用集合继承关系图

概述 集合类存放的都是对象的引用&#xff0c;而非对象本身&#xff0c;出于表达上的便利&#xff0c;我们称集合中的对象就是指集合中对象的引用。 类图如下&#xff1a; 1、Iterable与Iterator接口之间的区别 我看到好多网上的文章类图里面Collection 是继承Iterator接口&a…

【CodeForces - 673D】Bear and Two Paths(构造,tricks)

题干&#xff1a; Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities. Bear Limak was once in a city a and he wanted to go to a cit…