题意

5702 Count The Repetitions 0x50「动态规划」例题

描述

定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如:

conn("abc",2)="abcabc"

称字符串 a 能由字符串 b 生成,当且仅当从字符串 b 中删除某些字符后可以得到字符串 a。例如“abdbec”可以生成“abc”,但是“acbbe”不能生成“abc”。

给定两个字符串 s_1 和 s_2,以及两个整数 n_1 和 n_2,求一个最大的整数 m,满足conn(conn(s_2,n_2 ),m) 能由 conn(s_1,n_1) 生成。

s_1 和 s_2 长度不超过100,n_1 和 n_2 不大于 10^6。

输入格式

本题只有1个测试点,包含多组数据。每组数据由2行组成,第一行是s2,n2,第二行是s1,n1。

输出格式

对于每组数据输出一行表示答案m。

样例输入

ab 2
acb 4
acb 1
acb 1
aa 1
aaa 3
baab 1
baba 11
aaaaa 1
aaa 20

样例输出

2
1
4
7
12

来源

https://leetcode.com/problems/count-the-repetitions/description/

        </article>

分析

发现可以求出最大的m',满足conn(s2,m')能由conn(s1,n1)生成,那么答案m=m'/n2。

由于m'≤1e8不能直接F[i,j]表示从s1[i]开始要多少字符生成conn(s2,j),观察到m'可以用二进制拆分,所以利用倍增优化。

设F[i,j]表示从s1[i]开始至少需要多少字符才能生成conn(s2,\(2^j\))。状态转移方程为:

\[F[i,j]=F[i,j-1]+F[(i+F[i,j-1])\bmod |s_1|,j-1]
\]

可以暴力预处理F[i,0]。总时间复杂度\(O(|s_2||s_1|^2+|s_1|\log(|s_1|*n_1/|s_2|))\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
typedef long long ll;
using namespace std; string s1,s2;
int n1,n2;
ll f[100][28];
void Count_the_Repetitions(){
for(int i=0;i<s1.size();++i){
int pos=i;
f[i][0]=0;
for(int j=0;j<s2.size();++j){
int cnt=0;
while(s1[pos]!=s2[j]){
pos=(pos+1)%s1.size();
if(++cnt>=s1.size()) return cout<<0<<endl,void();
}
pos=(pos+1)%s1.size(),f[i][0]+=cnt+1;
}
}
for(int j=1;j<=27;++j)
for(int i=0;i<s1.size();++i)
f[i][j]=f[i][j-1]+f[(i+f[i][j-1])%s1.size()][j-1];
ll m=0;
for(int st=0;st<s1.size();++st){
ll x=st,ans=0;
for(int k=27;k>=0;--k)
if(x+f[x%s1.size()][k]<=s1.size()*n1)
x+=f[x%s1.size()][k],ans+=1<<k;
m=max(m,ans);
}
cout<<m/n2<<endl;
}
int main(){
ios::sync_with_stdio(0);
while(cin>>s2>>n2>>s1>>n1) Count_the_Repetitions();
return 0;
}

CH5702 Count The Repetitions的更多相关文章

  1. CH5702 Count The Repetitions[倍增dp]

    http://contest-hunter.org:83/contest/0x50%E3%80%8C%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E3%80%8D%E4%B ...

  2. [Swift]LeetCode466. 统计重复个数 | Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  3. 466. Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  4. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  5. Leetcode: Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  6. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  7. 第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)

    Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第i位开始匹配2^k个 ...

  8. [LeetCode] 466. Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  9. LeetCode466. Count The Repetitions

    题目链接 传送门 题意 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值 解题思路 注意s1与S1的区别, 可以去看题目描述, 预处 ...

随机推荐

  1. QT 设置应用程序名称和主窗口标题

    1.设置应用程序名称 在工程文件.pro文件中,修改Target为想设置的名称 TARGET = MXEditer 2.设置主窗口标题,在main文件中,我的主窗口是MainWindow. int m ...

  2. js parseInt

    语法: parseInt(string, radix); string 要被解析的值.如果参数不是一个字符串,则将其转换为字符串(使用  ToString 抽象操作).字符串开头的空白符将会被忽略. ...

  3. 雷林鹏分享:jQuery EasyUI 数据网格 - 启用行内编辑

    jQuery EasyUI 数据网格 - 启用行内编辑 可编辑的功能是最近添加到数据网格(datagrid)的.它可以使用户添加一个新行到数据网格(datagrid).用户也可以更新一个或多个行. 本 ...

  4. 实现mypwd&mybash&myod&读者写者

    目录: 一.mypwd 二.mybash 三.myod 四.读者.写者 一.实现mypwd 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试m ...

  5. js创建对象的三种方法

    1.使用对象初始化器:{} var person = {....} 2 var person=new object() function person(参数) { this.参数=... } var ...

  6. MongoDB一键安装

    #!/bin/bash export lang=Cecho '#1.关闭本地的MongoDB'#service mongodb stopecho '#2.清空本地MongoDB的安装文件'rm -rf ...

  7. 搜狗浏览器或者360浏览器安装chrome 浏览器插件

    https://www.cnblogs.com/ingvar/p/6403777.html#undefined

  8. 一直又爱又恨的jqueryValidate,看到一个还不错的laber.error样式

    默认样式,不是很好看 修改之后就高大上多了 功臣如下: label.error {    position: absolute;    right: 18px;    top: 5px;   colo ...

  9. Android 音视频深入 八 小视频录制(附源码下载)

    本篇项目地址,求starthttps://github.com/979451341/Audio-and-video-learning-materials/tree/master/%E5%B0%8F%E ...

  10. 关于规范NOIP试题管理办法的通知

    由CCF主办的NOIP赛事举行在即,保密起见,现将有关规定发给各省赛区组织单位. 1.NOI各省组织单位负责试题保密工作. 2.NOIP初赛试卷为纸质版,复赛试卷为电子版. 3.在初赛进行中,如有选手 ...