zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Android Studio 开发环境设置
    Android-项目介绍
    Android-开发工具
    在js 中使用ajax 调用后台代码方法,解析返回值
    $.each解析json
    VS2008 "当前不会命中断点。源代码与原始版本不同"解决方法
    64位系统 安装oracle
    session丢失返回登陆页
    DataTable转换为JsonResult
    easyui 绑定数据
  • 原文地址:https://www.cnblogs.com/Farrell-12138/p/13806403.html
Copyright © 2011-2022 走看看