Leecoder466 Count The Repetitons
Leecoder466 Count The Repetitons
题目大意
定义\([s,n]\)为连续\(n\)个串\(s\)构成的串
现在给定\(s_1,n_1,s_2,n_2\),求最大的\(m\)满足\([[s_2,n_2],m]\)是\([s_1,n_1]\)的子序列
\(|s_1|,|s_2| \le 100,n_1,n_2\le 10^6\)
首先,\([s_2,n_2]\)我们是不能直接求出来的
但是很明显\([[s_2,n_2],m] = [s_2,n_2m]\)
所以现在我们只需要求最大的\(m'\)使得\([s_2, m']\)为\([s_1,n_1]\)的子序列
那么就有
\]
我们发现,直接求\(m'\)是不容易实现的,因为他的上界很大,可能到达$\frac{|s_1| \times n_1}{n_2} $
考虑把\(m'\)二进制分解之后分位贪心
设\(f_{i,j}\)表示从\(s_1\)的第\(i\)位开始匹配\(2^j\)个\(s_2\)需要的字符个数
那么则有
\]
至于\(f_{i,0}\)我们可以使用最朴素的算法,直接暴力匹配,
注意判断无解的情况直接输出\(0\)就好了
这样我们就预处理完了
求答案就直接暴力枚举开头位置
然后利用倍增数组从高位向低位贪心,另外当前用了多少字符的初始值应该是\(i\)而不是\(0\)
class Solution {
int len1,len2;
char s1[505],s2[505];
long long f[31][505];
public:
int getMaxRepetitions(string S1, int n1, string S2, int n2) {
memset(f,0,sizeof(f));
len1 = S1.size();
len2 = S2.size();
for(int i = 0;i < len1;++i) s1[i] = S1[i];
for(int i = 0;i < len2;++i) s2[i] = S2[i];
// printf("%d %d\n",len1,len2);
for(int i = 0;i < len1;++i){
int pos = i;
for(int j = 0;j < len2;++j){
int cnt = 0;
while(s1[pos] != s2[j]){
pos = (pos + 1) % len1;
if(++cnt >= len1) return 0;
}
pos = (pos + 1) % len1;
f[0][i] += cnt + 1;
}
}
// for(int i = 0;i < len1;++i)printf("%d ",f[0][i]);puts("");
for(int j = 1;j <= 30;++j){
for(int i = 0;i < len1;++i){
f[j][i] = f[j - 1][i] + f[j - 1][(i + f[j - 1][i]) % len1];
}
}
long long ans = 0;
for(int i = 0;i < len1;++i){
long long t = i,sum = 0;
for(int j = 30;j >= 0;--j){
if((t + f[j][t % len1]) <= (len1) * n1){
t += f[j][t % len1];
sum += 1 << j;
}
}
//printf("%d %lld\n",i,sum);
ans = max(ans,sum);
}
return ans / n2;
}
};
Leecoder466 Count The Repetitons的更多相关文章
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- C#中Length和Count的区别(个人观点)
这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...
- [PHP源码阅读]count函数
在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...
- EntityFramework.Extended 实现 update count+=1
在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...
- 学习笔记 MYSQL报错注入(count()、rand()、group by)
首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...
- count(*) 与count (字段名)的区别
count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
随机推荐
- blogbeta1
//html <!DOCTYPE html> blog 身高:170 体重:230 座右铭 再给我吃一口 关于我 微信 微博 标签 SM SP 重金求爹 2019/11/16 本人找爹,带 ...
- Anaconda入门使用指南
打算学习 Python 来做数据分析的你,是不是在开始时就遇到各种麻烦呢? 到底该装 Python2 呢还是 Python3 ? 为什么安装 Python 时总是出错? 怎么安装工具包呢? 为什么提示 ...
- Failed to delete access_log
重复build 关闭已经开启的tomcat terminal 再次开启即可
- Codeforces 336C
这题是大一暑假时候做的,当时没有出,直到今天突然觉得应该把没过的题目再做一边,不然真的是越积越多. 现在能够独立做出来真的是难以表达的兴奋,刚开始的时候就觉得 O(30 * 30 * n)的复杂度有点 ...
- win7 powershell版本过低问题
那台win8系统的笔记本电脑 硬盘坏掉后 在win7系统的台式机上使用 vagrant up 提示版本过低 The version of powershell currently installed ...
- 数据分析1:安装tushare安装包
1. 2. 3.重点内容
- spring调试
spring-beans DefaultListableBeanFactory preInstantiateSingletons:650 RequestMappingHandlerMapping Ab ...
- Laravel请求和输入
该篇文章主要介绍Laravel获取用户请求和输入信息的方法.获取基本输入信息: //获取输入数据,不用担心所使用的HTTP方法 $id = Input::get('id'); //可以指定默认值 $i ...
- JavaScript 验证API
约束验证 DOM 方法 Property Description checkValidity() 如果 input 元素中的数据是合法的返回 true,否则返回 false. setCustomVal ...
- 2019-8-31-dotnet-core-输出调试信息到-DebugView-软件
title author date CreateTime categories dotnet core 输出调试信息到 DebugView 软件 lindexi 2019-08-31 16:55:58 ...