首页 > 其他分享 >回文相关

回文相关

时间:2022-12-24 19:11:19浏览次数:64  
标签:2000010 int res h1 h2 相关 回文

求最长回文串:回文子串的最大长度

AC 代码:(字符串 hash 解决)

#include <bits/stdc++.h>

using ULL = unsigned long long;
constexpr int P = 131;
char s[2000010];
ULL h1[2000010], h2[2000010], p[2000010];
int res;
void solve() {
    int n = strlen(s + 1) * 2;
    for (int i = n; i; i -= 2) {
        s[i] = s[i / 2];
        s[i - 1] = 'z' + 1;
    }
    s[++n] = 'z' + 1;

    for (int i = 1; i <= n; ++i) {
        h1[i] = h1[i - 1] * P + s[i] - 'a' + 1;
    }
    h2[n + 1] = 0;  // 防止上个样例影响这次运算
    for (int i = n; i; --i) {
        h2[i] = h2[i + 1] * P + s[i] - 'a' + 1;
    }
    p[0] = 1;
    for (int i = 1; i <= n; ++i) {
        p[i] = p[i - 1] * P;
    }

    auto cal1 = [&](int l, int r) -> ULL {  // 计算 h1 哈希
        return h1[r] - h1[l - 1] * p[r - l + 1];
    };
    auto cal2 = [&](int l, int r) -> ULL {  // 计算 h2 哈希
        return h2[l] - h2[r + 1] * p[r - l + 1];
    };

    for (int i = 1; i <= n; ++i) {
        int r = std::min(i - 1, n - i);
        if (res >= r || cal1(i - res, i - 1) != cal2(i + 1, i + res)) {
            continue;
        }
        while (res <= r && cal1(i - res, i - 1) == cal2(i + 1, i + res)) {
            res++;
        }
        res--;
    }
}
int main() {
    for (int T = 1; ; ++T) {
        res = 1;
        scanf("%s", s + 1);
        if (strcmp(s + 1, "END")) {
            solve();
            printf("Case %d: %d\n", T, res);
        } else {
            break;
        }
    }

    return 0;
}

回文子序列:密码脱落

AC 代码:

#include <bits/stdc++.h>

int main() {
    std::string s;
    std::cin >> s;
    int n = s.size();
    s = " " + s;

    std::vector<std::vector<int>> dp(n + 1, std::vector<int>(n + 1, 0));
    for (int i = 1; i <= n; ++i) {
        dp[i][i] = 1;
    }
    for (int len = 2; len <= n; ++len) {
        for (int l = 1; l + len - 1 <= n; ++l) {
            int r = l + len - 1;
            dp[l][r] = std::max(dp[l + 1][r], dp[l][r - 1]);
            if (s[l] == s[r]) {
                dp[l][r] = dp[l + 1][r - 1] + 2;
            }
        }
    }
    std::cout << n - dp[1][n] << '\n';

    return 0;
}

树上求最长回文子序列:Hossam and (sub-)palindromic tree
AC 代码:


标签:2000010,int,res,h1,h2,相关,回文
From: https://www.cnblogs.com/hacker-dvd/p/17003227.html

相关文章

  • 八、进程相关函数
    相关函数:abort头文件 :#include<stdlib.h>函数原型:voidabort(void);函数说明:引起进程异常终止,此时所有已打开的文件流会自动关闭,缓冲区里的数据也会自动写回返......
  • 相关函数: memcmp
    头文件 :#include<string.h>函数原型:intmemcpy(constvoid*s1, constvoid*s2, size_tn);函数说明:比较s1和s2所指向内存区域前n个字节返回值 :若完全相......
  • 相关函数: memcpy
    相关函数:memcpy头文件 :#include<string.h>函数原型:void*memcpy(void*dest, constvoid*src, size_tn);函数说明:拷贝src所指向的内存前n个字节到dest所......
  • 相关函数: strncat
    头文件 :#include<string.h>函数原型:char*strncat(char*dest, constchar*src, size_tn);函数说明:将参数str指向的字符串拷贝n个字符到参数dest所指向的字......
  • 相关函数: strncmp
    头文件 :#include<string.h>函数原型:intstrncmp(constchar*s1, constchar*s2, size_tn);函数说明:比较参数s1和s2所指向的字符串的前n个字符返回值 :若......
  • 相关函数: strcmp
    头文件 :#include<string.h>函数原型:intstrcmp(constchar*s1, constchar*s2);函数说明:比较参数s1和s2所指向的字符串返回值 :若字符串相等则返回0;若字符......
  • 字符串处理函数 相关函数: strstr
    相关函数:strstr头文件 :#include<string.h>函数原型:char*strstr(constchar*haystack, constchar*needle);函数说明:在字符串haystack中查找字符串needle......
  • 解决OpenVINO由R2019_1到R2019_2 升级带来的相关的问题
    OpenVINO提供了丰富的例子,为了方便研究和使用,我们需要将这些例子由原始的demo目录中分离出来,也就是“独立”运行,这里我们选择了较为简单的super_resolution_demo来说......
  • OpenGL相关文档
    1)OpenGLAPIhttps://docs.gl/gl4/glUniform1-1)纹理:https://blog.csdn.net/ht_vIC/article/details/123699253https://zhuanlan.zhihu.com/p/146436546https://www.cnblog......
  • 模型admin 外键的相关操作
    [email protected](MyModel)classMyModelAdmin(admin.ModelAdmin):defmethod(self,request,queryset):#获取外键关联模型f类型为<class'django.db.models......