题目链接:MAXMATCH - Maximum Self-Matching

Description

You're given a string s consisting of letters 'a', 'b' and 'c'.

The matching function \(m_s( i )\) is defined as the number of matching characters of s and its i-shift. In other words, \(m_s( i )\) is the number of characters that are matched when you align the 0-th character of s with the i-th character of its copy.

You are asked to compute the maximum of \(m_s( i )\) for all i ( 1 <= i <= |s| ). To make it a bit harder, you should also output all the optimal i's in increasing order.

Input

The first and only line of input contains the string s. \(2 \le |s| \le 10^5\).

Output

The first line of output contains the maximal \(m_s( i )\) over all i.

The second line of output contains all the i's for which \(m_s( i )\) reaches maximum.

Example

Input:
caccacaa Output:
4
3 Explanation: caccacaa
caccacaa The bold characters indicate the ones that match when shift = 3.

Solution

题意

给定一个字符串 \(s\) (下标从 \(0\) 开始,只包含 'a', 'b', 'c'),让 \(s\) 与 \(s\) 匹配,下标从 \(1\) 移动到 \(|s|\),每次匹配时的相同的字符个数记为 \(m_s( i )\),求 \(m_s( i )\) 的最大值,以及最大值所匹配的所有位置。

比如 ababa

ababa
ababa
ababa
ababa
ababa

\(m_s( i )\) 分别为 \(0, 3, 0, 1\),最大值为 \(3\)。

思路

FFT

字符串匹配问题。

设模式串为 \(p\),目标串为 \(t\),\(f[k]\) 为模式串从目标串第 \(k\) 位开始匹配的结果。

对 \(a\),\(b\),\(c\) 分开求。

首先判断 \(a\) 的情况,将字符串转化为 01 串,比如 ababa 转为 10101。

那么

\[f[k] = \sum_{i=0}^{|s|-k-1} p[i] \cdot t[k + i]
\]

\[f[k] = \sum_{i=k}^{|s|-1} p[i - k] \cdot t[i]
\]

将模式串倒置,有

\[f[k] = \sum_{i=k}^{|s|-1} p[|s| - 1 - i + k] \cdot t[i]
\]

令 \(j = |s| - 1 - i + k\),有

\[f[k] = \sum_{i+j=|s|-1+k} p[j] \cdot t[i]
\]

用 \(FFT\) 求一下卷积即可。

对于 \(b\) 和 \(c\) 的求法相同。

Code

#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-8;
typedef complex<double> Complex;
const int maxn = 2e6 + 10; Complex p[maxn], t[maxn];
Complex a[maxn], b[maxn], c[maxn];
int ans[maxn];
string str;
int n;
int bit = 2, rev[maxn]; void get_rev(){
memset(rev, 0, sizeof(rev));
while(bit <= n + n) bit <<= 1;
for(int i = 0; i < bit; ++i) {
rev[i] = (rev[i >> 1] >> 1) | (bit >> 1) * (i & 1);
}
} void FFT(Complex *arr, int op) {
for(int i = 0; i < bit; ++i) {
if(i < rev[i]) swap(arr[i], arr[rev[i]]);
}
for(int mid = 1; mid < bit; mid <<= 1) {
Complex wn = Complex(cos(PI / mid), op * sin(PI / mid));
for(int j = 0; j < bit; j += mid<<1) {
Complex w(1, 0);
for(int k = 0; k < mid; ++k, w = w * wn) {
Complex x = arr[j + k], y = w * arr[j + k + mid];
arr[j + k] = x + y, arr[j + k + mid] = x - y;
}
}
}
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> str;
n = str.size();
for(int i = 0; i < n; ++i) {
p[n - i - 1] = str[i] == 'a' ? 1 : 0;
t[i] = p[n - i - 1];
}
get_rev();
FFT(p, 1); FFT(t, 1);
for(int i = 0; i < bit; ++i) {
a[i] = p[i] * t[i];
}
FFT(a, -1); for(int i = 0; i < n; ++i) {
p[n - i - 1] = str[i] == 'b' ? 1 : 0;
t[i] = p[n - i - 1];
}
for(int i = n; i < bit; ++i) {
p[i] = 0;
t[i] = 0;
}
FFT(p, 1); FFT(t, 1);
for(int i = 0; i < bit; ++i) {
b[i] = p[i] * t[i];
}
FFT(b, -1); for(int i = 0; i < n; ++i) {
p[n - i - 1] = str[i] == 'c' ? 1 : 0;
t[i] = p[n - i - 1];
}
for(int i = n; i < bit; ++i) {
p[i] = 0;
t[i] = 0;
}
FFT(p, 1); FFT(t, 1);
for(int i = 0; i < bit; ++i) {
c[i] = p[i] * t[i];
}
FFT(c, -1); int maxa = 0;
for(int i = 1; i < n; ++i) {
ans[i] = (int)(a[n - 1 + i].real() / bit + 0.5) + (int)(b[n - 1 + i].real() / bit + 0.5) + (int)(c[n - 1 + i].real() / bit + 0.5);
maxa = max(maxa, ans[i]);
}
vector<int> pos;
for(int i = 1; i < n; ++i) {
if(ans[i] == maxa) {
pos.push_back(i);
}
}
cout << maxa << endl;
for(int i = 0; i < pos.size(); ++i) {
cout << pos[i] << " ";
}
cout << endl; return 0;
}

SPOJ MAXMATCH - Maximum Self-Matching (FFT)的更多相关文章

  1. Maximum Bipartite Matching

    算法旨在用尽可能简单的思路解决这个问题.理解算法也应该是一个越看越简单的过程,当你看到算法里的一串概念,或者一大坨代码,第一感觉是复杂,此时最好还是从样例入手.通过一个简单的样例,并编程实现,这个过程 ...

  2. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...

  3. SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

    SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...

  4. 2018.11.18 spoj Triple Sums(容斥原理+fft)

    传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai​,aj​,ak​( ...

  5. 牛客网暑期ACM多校训练营(第三场)DEncrypted String Matching fft

    题意:给你一个解密后的字符串,给你加密方式,加密过程可能出错,字符可能加减1,然后给你一个字符串,要求匹配个数(其实我也不太懂具体怎么加密解密,反正你把给你的前两个字符串用第三个加密一下,然后搞可以有 ...

  6. SPOJ VFMUL - Very Fast Multiplication (FFT)

    题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...

  7. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  8. 转债---Pregel: A System for Large-Scale Graph Processing(译)

    转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/   作者:Grzegorz Malewicz, Matthew ...

  9. Pregel: A System for Large-Scale Graph Processing(译)

    [说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...

随机推荐

  1. PHP错误检测

    开发的时候,我们有时候需要打开错误信息.这时候,可以在php文件里设置:ini_set('display_errors','on');error_reporting(E_ALL); 不过有时候我们及时 ...

  2. 【C++第一个Demo】---控制台RPG游戏1【游戏简介】

       经过1个月的制作和多次修改,终于有了基本雏形(此篇仅用于纪念历时3个多月C/C++学习所付出努力,也给和我一样苦恼于不能快速理解面向对象的同学们一点灵感) 在制作这个Demo过程中也受到了很多大 ...

  3. java 重新学习 (五)

    Set 集合 一.HashSet按照Hash算法存储集合元素(hashCode方法获取hashCode值,根据hashCode值获取元素位置,通过equals判断对象是否相等并且hashCode值是否 ...

  4. scala解析json —— json4s 解析json方法汇总

    使用json4s的框架,包括spark,flink 1.org.json4s 引入pom的方法 对于本地支持,引入以下依赖项添加到pom中 <dependency> <groupId ...

  5. 安装node --- 与升级

    1.安装node流程,请参照菜鸟教程的安装教程   http://www.runoob.com/nodejs/nodejs-install-setup.html 2.这里说一下踩的坑, 问题一.Win ...

  6. JAVA计算整数的位数

    /** * 计算整数的位数 * @param x * @return */ public static int countIntegerLength(int x){ final int [] size ...

  7. vue 中引入cryptoJS

    在搞前端开发的时候,页面上有很多的地方是需要用户输入信息的,但是有些信息又很敏感,比如客户的姓名.电话号码.身份证号码.银行卡号及密码等等这些,如果没有进行加密处理,很容易被别人截取到,项目中应用到c ...

  8. 【记录】Nginx错误could not build the server_names_hash you should increase server_names_hash_bucket_size: 32

    今天遇到这个错误,现记录下解决方案: 在nginx的配置文件的http段中增加如下配置: server_names_hash_bucket_size 64; 下面是nginx官方文档解释: 如果定义了 ...

  9. static变量与普通变量的异同

    1.static局部变量与普通局部变量的异同 相同点:都是局部变量,在函数内部定义,仅能被该模块内部的语句所访问. 不同点: 1)内存分配与释放: static修饰的局部变量在内存中存放在静态存储区, ...

  10. mongodb的学习 (3)

    聚合函数 - 添加基础数据:db.local.save({contry:'中国',name:'小明',score:77});db.local.save({contry:'中国',name:'小红',s ...