AcWing 139. 回文子串的最大长度

本文介绍了一种使用字符串哈希和二分搜索算法来寻找给定字符串中最长回文子串的方法。通过构建哈希数组,算法能在O(n)的时间复杂度内找到最长回文子串的长度。

知识点:字符串哈希+二分

如果一个字符串正着读和倒着读是一样的,则称它是回文的。

给定一个长度为N的字符串S,求他的最长回文子串的长度是多少。

输入格式

输入将包含最多30个测试用例,每个测试用例占一行,以最多1000000个小写字符的形式给出。

输入以一个以字符串“END”(不包括引号)开头的行表示输入终止。

输出格式

对于输入中的每个测试用例,输出测试用例编号和最大回文子串的长度(参考样例格式)。

每个输出占一行。

输入样例:

abcbabcbabcba
abacacbaaaab
END

输出样例:

Case 1: 13
Case 2: 6

import java.io.*;
import java.lang.*;
import java.util.*;
class Main{
    
    static int N = 2000010;
    static long[] hl = new long[N], hr = new long[N], p = new long[N];
    static int P = 131;
    
    static long get(long[] h, int l, int r){//计算哈希值
        return h[r] - h[l - 1] * p[r - l + 1];
    }
    public static void main(String[] args)throws Exception{
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String chs_new;
        int t = 1;
        while(!"END".equals(chs_new = buf.readLine())){
            int n = chs_new.length();
            char[] chs = chs_new.toCharArray();
            char[] str = new char[2 * n + 1];
            for(int i = 1; i < n + 1; ++i){
                str[i] = chs[i - 1];
            }
            n *= 2;
            for(int i = n; i > 0; i -= 2){
                str[i] = str[i / 2];
                str[i - 1] = 'z' + 1;
            }
            
            // for(int i = 1; i <= n; ++i){
            //     System.out.print(str[i]);
            // }
            
            // System.out.println();
            p[0] = 1;
            for(int i = 1, j = n; i <= n; ++i, --j){//构建哈希数组
                p[i] = p[i - 1] * P;
                hl[i] = hl[i - 1] * P + str[i] - 'a' + 1;
                hr[i] = hr[i - 1] * P + str[j] - 'a' + 1;
            }
            int max = 0;
            for(int i = 1; i <= n; ++i){
                int l = 0, r = Math.min(i - 1, n - i);//半径的最小最大值
                while(l < r){//选择哪种二分方法 要按照题目需要,在此处选择第二种合适
                    int mid = l + r + 1 >> 1;
                    if(get(hl, i - mid, i - 1) != get(hr, n - (i + mid) + 1, n - (i + 1) + 1 ))r = mid - 1;
                    else l = mid;
                }
                if(str[i - l] <= 'z')
                    max = Math.max(max, l + 1);
                else
                    max = Math.max(max, l);
            }
            System.out.printf("Case %d: %d\n", t++, max);
        }
    }
}

 

AcWing 802. 区间问题需要用到离散化的方法来解决。离散化的本质是映射,将间隔很大的点,映射到相邻的数组元素中,以此减少对空间的需求计算量。 之所以采用离散化,原因有两点:一是存储的下标太大,直接开这么大的数组不现实;二是数轴上的下标可能存在负值,无法直接使用数组下标。同时,哈希表也不适合解决此问题,因为它不能像离散化那样缩小数组空间,且不能排序,难以提前知道数轴上哪些点存在,需要从负的最小值到正的最大值都枚举一遍,时间复杂度太高 [^1]。 在处理过程中,对于去重操作,可使用 `unique` 函数 `erase` 函数。`unique` 函数会将容器中不重复的元素移到前面,但其本身不会真正删除重复元素,容器长度不变,只是元素位置改变。它返回指向去重后数组尾端点的迭代器。而 `erase` 函数能真正从容器中去除重复元素,使容器长度发生变化,所以通常将二者配合使用来达到删除重复元素的目的 [^2]。 以下是解决该问题可能会涉及的代码思路示例(C++): ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef pair<int, int> PII; const int N = 300010; int n, m; int a[N], s[N]; vector<int> alls; vector<PII> add, query; // 二分查找离散化后的位置 int find(int x) { int l = 0, r = alls.size() - 1; while (l < r) { int mid = l + r >> 1; if (alls[mid] >= x) r = mid; else l = mid + 1; } return r + 1; } int main() { cin >> n >> m; for (int i = 0; i < n; i ++ ) { int x, c; cin >> x >> c; add.push_back({x, c}); alls.push_back(x); } for (int i = 0; i < m; i ++ ) { int l, r; cin >> l >> r; query.push_back({l, r}); alls.push_back(l); alls.push_back(r); } // 去重 sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end()); // 处理插入 for (auto item : add) { int x = find(item.first); a[x] += item.second; } // 预处理前缀 for (int i = 1; i <= alls.size(); i ++ ) s[i] = s[i - 1] + a[i]; // 处理询问 for (auto item : query) { int l = find(item.first), r = find(item.second); cout << s[r] - s[l - 1] << endl; } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值