• Codeforces —— Palindromic characteristics(835D)


    input
    abba
    output
    6 1 0 0 
    
    input
    abacaba
    output
    12 4 1 0 0 0 0 
    

    题意:

    1-palindromes:该字符串为一个回文字符串
    k-palindromes满足:
    1.该字符串为一个回文字符串
    2.其左边以及右边是一个(k-1)-palindromes
    询问给定字符串中有几个i-palindromes;
    

    思路:

    考虑区间DP,dp[i][j] —— 字符串i~j是最多是几阶回文
    状态转移方程:dp[i][j] = dp[i][i+len/2-1] + 1;
    

    代码

    #include<unordered_map>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<map>
    #include<set>
    #define Buff ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #define rush() int Case = 0; int T; scanf("%d", &T);  while(T--)
    #define rep(i, a, b) for(int i = a; i <= b; i ++)
    #define per(i, a, b) for(int i = a; i >= b; i --)
    #define reps(i, a, b) for(int i = a; b; i ++)
    #define clc(a, b) memset(a, b, sizeof(a))
    #define Buff ios::sync_with_stdio(false)
    #define readl(a) scanf("%lld", &a)
    #define readd(a) scanf("%lf", &a)
    #define readc(a) scanf("%c", &a)
    #define reads(a) scanf("%s", a)
    #define read(a) scanf("%d", &a)
    #define lowbit(n) (n&(-n))
    #define pb push_back
    #define sqr(x) x*x
    #define rs x<<1|1
    #define y second
    #define ls x<<1
    #define x first
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int>PII;
    const int mod = 1e9+7;
    const double eps = 1e-6;
    const int N = 5e3+7;
    int res[N], f[N][N];
    char s[N];
    int main()
    {
    	Buff;
    	cin >> s+1;
    	int n = strlen(s+1);
    	res[1] = n;
    	rep(i, 1, n)	f[i][i] = 1;
    	for(int len = 2; len <= n; len ++)
    		for(int l = 1; l + len - 1 <= n; l ++)
    		{
    			int r = l + len - 1;
    			if(s[l] != s[r] || (l+1 <= r-1) && !f[l+1][r-1])	f[l][r] = 0;
    			else												f[l][r] += f[l][l+len/2-1] + 1;
    			res[f[l][r]] ++;
    		}
    	per(i, n-1, 1)	res[i] += res[i+1];
    	rep(i, 1, n)	cout << res[i] <<" ";puts("");
    	return 0;
    }
    
  • 相关阅读:
    c++链表实现学生成绩管理系统(简易版)
    IOS动画讲解
    栈的实现
    Masonry的使用
    二叉树详解-2
    二叉树详解-1
    CoreData的使用-2
    NSPredicate 详解
    CoreData的使用-1
    IOS常用手势用法
  • 原文地址:https://www.cnblogs.com/Farrell-12138/p/13806403.html
Copyright © 2020-2023  润新知