SPOJ687---REPEATS - Repeats(后缀数组+RMQ)

交叉编译Qt6.5.3(RK3568开发板) 注意,如果使用其它版本的交叉编译器的话,请确保你的交叉编译器能够支持 C++17标准。为确保后续交叉编译没问题,需要确保开发板使用的GLIBC版本不小于2.34版本。到 linaro 网站上下载交叉编译工具,我下载的是 gcc-linaro-11.3.1-2022.06-x86_64_aarch64-linux-gnu.tar.xz。安装 CMake ,最低要求3.16版本,如果要静态编译,需要3.21及以上版本。我使用的3.27版本。:指定为最后交叉编译后的Qt6.5.3库的安装目录。 阅读详情

A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string

s = abaabaabaaba

is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.

Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string

u = babbabaabaabaabab

contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.

Input

In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.
Output

For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.
Example

Input:
1
17
b
a
b
b
a
b
a
a
b
a
a
b
a
a
b
a
b

Output:
4

since a (4, 3)-repeat is found starting at the 5th character of the input string.

方法还是论文上说的方法,我用2个后缀数组,另外一个是字符串反转求出来的,然后分别LCP

/*************************************************************************
    > File Name: SPOJ-687.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月07日 星期二 18时16分49秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

class SuffixArray
{
    public:
        static const int N = 52000;
        int init[N];
        int X[N];
        int Y[N];
        int Rank[N];
        int sa[N];
        int height[N];
        int buc[N];
        int LOG[N];
        int dp[N][20];
        int size;

        void clear()
        {
            size = 0;
        }

        void insert(int n)
        {
            init[size++] = n;
        }

        bool cmp(int *r, int a, int b, int l)
        {
            return (r[a] == r[b] && r[a + l] == r[b + l]);
        }

        void getsa(int m = 256) //m一般为最大值+1
        {
            init[size] = 0;
            int l, p, *x = X, *y = Y, n = size + 1;
            for (int i = 0; i < m; ++i)
            {
                buc[i] = 0;
            }
            for (int i = 0; i < n; ++i)
            {
                ++buc[x[i] = init[i]];
            }
            for (int i = 1; i < m; ++i)
            {
                buc[i] += buc[i - 1];
            }
            for (int i = n - 1; i >= 0; --i)
            {
                sa[--buc[x[i]]] = i;
            }
            for (l = 1, p = 1; l <= n && p < n; m = p, l *= 2)
            {
                p = 0;
                for (int i = n - l; i < n; ++i)
                {
                    y[p++] = i;
                }
                for (int i = 0; i < n; ++i)
                {
                    if (sa[i] >= l)
                    {
                        y[p++] = sa[i] - l;
                    }
                }
                for (int i = 0; i < m; ++i)
                {
                    buc[i] = 0;
                }
                for (int i = 0; i < n; ++i)
                {
                    ++buc[x[y[i]]];
                }
                for (int i = 1; i < m; ++i)
                {
                    buc[i] += buc[i - 1];
                }
                for (int i = n - 1; i >= 0; --i)
                {
                    sa[--buc[x[y[i]]]] = y[i];
                }
                int i;

                for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i)
                { 
                    x[sa[i]] = cmp(y, sa[i - 1], sa[i], l) ? p - 1 : p++; 
                }
            }
        }

        void getheight()
        {
            int h = 0, n = size;
            for (int i = 0; i <= n; ++i)
            {
                Rank[sa[i]] = i;
            }
            height[0] = 0;
            for (int i = 0; i < n; ++i)
            {
                if (h > 0)
                {
                    --h;
                }
                int j =sa[Rank[i] - 1];
                for (; i + h < n && j + h < n && init[i + h] == init[j + h]; ++h);
                height[Rank[i] - 1] = h;
            }
        }   

        //预处理每一个数字的对数,用于rmq,常数优化
        void initLOG()
        {
            LOG[0] = -1;
            for (int i = 1; i < N; ++i)
            {
                LOG[i] = (i & (i - 1)) ? LOG[i - 1] : LOG[i - 1] + 1;
            }
        }

        void initRMQ()
        {
            initLOG();
            int n = size;
            int limit;
            for (int i = 0; i < n; ++i)
            {
                dp[i][0] = height[i];
            }
            for (int j = 1; j <= LOG[n]; ++j)
            {
                limit = n - (1 << j);
                for (int i = 0; i <= limit; ++i)
                {
                    dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int LCP(int a, int b)
        {
            int t;
            a = Rank[a];
            b = Rank[b];
            if (a > b)
            {
                swap(a, b);
            }
            --b;
            t = LOG[b - a + 1];
            return min(dp[a][t], dp[b - (1 << t) + 1][t]);
        }
}SA1, SA2;

char buf[52000];
char str[10];

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        SA1.clear();
        SA2.clear();
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
        {
            scanf("%s", str);
            SA1.insert((int)str[0]);
            buf[i] = str[0];
        }
        buf[n] = '\0';
        for (int i = n - 1; i >= 0; --i)
        {
            SA2.insert((int)buf[i]);
        }
        SA1.getsa((int)('b') + 1);
        SA2.getsa((int)('b') + 1);
        SA1.getheight();
        SA2.getheight();
        SA1.initRMQ();
        SA2.initRMQ();
        int ans = 1, s, ans_L = 0;
        for (int L = 1; L <= n; ++L)
        {
            for (int i = 0; i < n; ++i)
            {
                int a = L * i;
                int b = L * (i + 1);
                if (b >= n)
                {
                    break;
                }
                int l1 = SA1.LCP(a, b);
                int l2 = SA2.LCP(n - a - 1, n - b - 1);
                int k = l1 + l2;
                if (k > 0)
                {
                    --k;
                }
                int cnt = k / L + 1;
                if (cnt > ans)
                {
                    ans = cnt;
                    s = a;
                    ans_L = L;
                } 

            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
Ardupilot二次开发(二):自定义mavlivk消息 在下面的文件添加自定义消息的xml定义然后编译固件,会生成相应的消息头文件。 阅读详情

相关推荐

使用WSL2安装ubuntu系统并配置深度学习环境

wsl2安装ubuntu部署深度学习环境的方法

u013358093的博客 2701

SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解

题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[0]、s[L]、s[2L] \cdots s[L * k]\)中至少有两个连续的位置是相同的,然后看字母\(s[L * i]和s[L * (i + 1)]\)往前往后最多能匹配多远,记住总长...

weixin_30412577的博客 284

嵌入式开发 DSP芯片运行方式(挖坑-填坑中-坑已平 :))

TMS320F28335 DSP 芯片 如何将运行代码放在不同的位置

FOOLCODE的专栏 1306

spoj687 REPEATS

传送门:http://www.spoj.com/problems/REPEATS/ 思路:又是一道论文题。 论文上是这么说的 这题用到了枚举长度的方法,往后匹配我们只要看两个后缀的LCP即可,而往前匹配,一个显然的做法是把串反过来,接到后面看对应的后缀的LCP。实际上并不需要这样,我们只需要看它还差几个字符就可以是循环次数加1,那我们就往前移这么多位,看这两个后缀的LCP长度...

weixin_30594001的博客 145

Megatron单机多卡Qwen2.5:pre-train+sft

英伟达:https://github.com/NVIDIA/Megatron-LM/releases/tag/core_r0.8.0阿里:https://github.com/alibaba/Pai-Megatron-Patch/releases/tag/v0.10.2。

weixin_46873015的博客 2179

S32K3 eMIOS输入捕获(ICU)全解析:从信号测量到代码实现

本文深入解析了S32K3 eMIOS输入捕获(ICU)技术,从信号测量到代码实现的完整流程。详细对比了六种核心捕获模式的性能差异,提供了动态模式切换、Counter Bus配置、中断服务程序优化等实战技巧,并分享了汽车电子中的典型应用案例,如发动机转速测量和CAN唤醒脉冲检测。

weixin_33137081的博客 347

Repeats SPOJ - REPEATS

A string s is called an (k,l)-repeat if s is obtained by concatenating k&gt;=1 times some seed string t with length l&gt;=1. For example, the strings = abaabaabaabais a (4,3)-repeat with t = aba as it...

海绵的博客 382

光猫改桥接后,一根网线搞定IPTV和上网的保姆级教程(附VLAN配置避坑点)

本文详细介绍了光猫改桥接后如何通过单线复用技术实现IPTV和上网功能共存。从VLAN配置原理到具体操作步骤,包括光猫超级管理员权限获取、VLAN绑定设置及路由器IPTV功能配置,提供了全面的保姆级教程。特别针对常见问题如IPTV信号丢失、网速下降等给出了实用解决方案,帮助用户轻松避坑。

weixin_29927965的博客 1760

spoj687 REPEATS - Repeats (后缀数组+rmq)

A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string s = abaabaabaaba is a (4,3)-repeat with t = aba as

新月 638

AUTOSAR&UDS 理论要点及isolar实战-DEM模块

从这节开始,将讲解UDS诊断中非常重要的知识点:故障事件触发、故障信息上报、故障信息处理、故障信息存储和故障系统降级,后续会陆续更新。

cqaa11的博客 552

RSA算法详解:万字文章详解RSA的加密与解密

为了彻彻底底的研究明白RSA算法,真的是。。煞(lei)费(si)苦(wo)心(le),首先我希望自己前前后后、彻彻底底的琢磨明白,其次我也希望我把他分享出来以后,大家都能读得懂,这篇文章真的是我迄今为止写的又臭又长的一篇文章了,但是我能保证的是,你读完了以后,绝对不会再问RSA是啥玩意儿了。。

布啦啦 7668

spoj687后缀数组-5

http://www.spoj.pl/problems/REPEATS/ 题意:给你一个串,求这个串中所有连续重复子串重复的最大次数。。如cababab,为3。。。 分析: 这题纯粹看的罗大牛的论文的,还没怎么看懂,还百度了报告。。我是想不到方法了。。 只考虑重复次数

xuanflyer 959

YOLO26改进策略【YOLO和Mamba】| MLLA:Mamba-Like Linear Attention,融合Mamba设计优势的注意力机制

本文记录的是利用模块优化的目标检测网络模型。模块具有独特优势。它不同于传统模块,能同时兼顾局部特征高效建模与长距离交互学习。常见模块要么在局部特征处理上有优势但长距离交互能力弱,要么反之,而模块克服了此问题。它融合了Mamba模型和线性注意力机制的优势,通过独特的结构设计,能够在保持计算效率的同时,精准地建模局部特征并学习长距离交互信息。本文将其用于的模型改进和二次创新,能够更加关注图像中的重要特征区域,抑制背景等无关信息的干扰,从而突出目标物体的关键特征。Demystify Mamba in Vision

Limiiiing的博客 213

SPOJ 687:REPEATS——后缀数组+RMQ

题面   洛谷 解析   先奉上YYR的PPT      基本思路这张PPT已经讲清楚了,但还有一些其他的细节   我们确定了$L*t$与$L*(t+1)$后,显然不能暴力向后跳或向前跳。考虑向后跳最多跳$LCP(L*t, L*(t+1))$个点,因此这个可以用后缀数组+RMQ预处理出来,快速查询。再考虑向前跳,向前跳最多产生1个循环节,因为如果有多个循环节的话...

weixin_30532973的博客 165

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

SPOJ 687 REPEATS Repeats 后缀数组 + RMQ预处理

题目大意: 对于给出的字符串(长度 大致思路: 就是POJ 3693那题的简单版本....不需要找到字典序最小的.... 首先有这样一个事实: 对于任何一个子串, repetition number >= 1, 所以对于repetition number为1的只需要找到字典序最小的那个字母即可, 那么我们只考虑repetition number >= 2的情况, 如果每一个循环节的

Gatevin的专栏 1200

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

SPOJ 687 Repeats 后缀数组+暴力+rmq

题意:给出一个字符串,求出一个连续的

leodestiny 557

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

SPOJ 687 Repeats (后缀数组+RMQ 重复次数最多的连续重复子串)

题意:给定一个字符串,求重复次数最多的连续重复子串。   算法分析:   先穷举长度L,然后求长度为L的子串最多能连续出现几次。首先连续出现1次是肯定可以的,所以这里只考虑至少2次的情况。假设在原字符串中连续出现2次,记这个子字符串为S,那么S肯定包括了字符r[0],r[L],r[L*2],r[L*3],……中的某相邻的两个。所以只须看字符r[L*i]和r[L*(i+1)]往前和往后各能匹配到

whyorwhnt的专栏 1147

SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)

REPEATS - Repeats no tags  A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string s = abaabaabaaba

Jogging_Clown 815

spoj 687 Repeats//后缀数组

SPOJ Problem Set (classical)687. RepeatsProblem code: REPEATSA string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the strings = abaabaabaabais a (4,3)-repeat with t = aba as its

hqd_acm的专栏 1493

SPOJ - 687 REPEATS(后缀数组)

题意: 求重复次数最多的连续重复子串的长度 题解: 其实上一篇已经写过这种题【POJ-3693】了,只不过那题最后是要求解最后的串,这题只需要求出循环次数即可。 一样的思路,枚举重复长度为L,就有rk[pos]rk[pos]rk[pos]和rk[pos+L]rk[pos+L]rk[pos+L]这两个串,pos为枚举的L的倍数,即rk[k∗L]rk[k*L]rk[k∗L]和rk[(k+1)∗L)]r...

qq_43544481的博客 228

SPOJ687 Repeats

本篇是罗穗骞《后缀数组——处理字符串的有力工具》的读书笔记。 知识点:  后缀数组、RMQ 解题思路:   枚举长度 \(L\),然后检查长度为 \(L\) 的子串最多能连续重复几次。   对于给定的字符串 \(S\),我们只关注其上坐标为 \(0, L, 2L, ......\) 的点。   如果连续重复子串的首字符恰好在这些点上,那么连续重复子串出现的次数恰好是 \( \f...

dielie6462的博客 250

Repeats spoj 687 后缀数组求重复次数最多的连续重复子串

A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string s = abaabaabaaba is a (4,3)-repeat with t = aba ...

Top_Spirit的博客 447

SPOJ 687. Repeats(后缀数组)

SPOJ 687. Repeats(后缀数组) 题意:给你一个长N的字符串,现在要你找出该字符串中具有最大循环节数目的连续子串.输出该子串的循环节数目即可. 分析:罗穗骞《后缀数组——处理字符串的有力工具》论文例题,不过论文中讲解的比较难懂. 首先还是引用论文的那段: 首先要明白上图其实就是用KMP算法自匹配循环节的标准图形. 难懂的地方在这里: 1.    什么时候可以往左匹配?

code 1554

Repeats (spoj 687)

题意:给你一串字符串,求重复次数最多的连续重复子串。例如u=babbabaabaabaababu = babbabaabaabaabab, 最多的为abaaba, 在u的子串abaabaabaabaabaabaabaaba中重复了4次。题解:参考论文上的解法,先枚举长度ll,然后求长度为ll的子串至少连续出现2次的情况。如果在原字符串中连续出现2次,那么这个由长为ll的子串重复两次所形成的串肯定包括

sdut16szq的博客 391

SPOJ REPEATS Repeats (后缀数组:子串的最大循环节)题解

题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[0]、s[L]、s[2L] \cdots s[L * k]\)中至少有两个连续的位置是相同的,然后看字母\(s[L * i]和s[L * (i + 1)]\)往前往后最多能匹配多远,记住总长...

weixin_30398227的博客 185

[SPOJ687]REPEATS - Repeats(后缀数组+st)

题目描述传送门 题意:给定一个字符串,求重复次数最多的连续重复子串。题解先枚举长度 L ,然后求长度为 L 的子串最多能连续出现几次。首先连续出现 1 次是肯定可以的,所以这里只考虑至少 2 次的情况。假设在原字符串中连续出现 2 次,记这个子字符串为 S ,那么 S 肯定包括了字符 r[0], r[L], r[L*2], r[L*3], …… 中的某相邻的两个。所以只须看字符 r[L*i] 和

zyf2000 867
上一篇: POJ1226---Substrings(后缀数组+二分)
下一篇: SPOJ220---Relevant Phrases of Annihilation(后缀数组+二分,对后缀分组)
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值