URAL 1635. Mnemonics and Palindromes (区间dp)

URAL 1635. Mnemonics and Palindromes(区间dp)

题目链接
题目大意:给定一个字符串,问分割成最少的子串,是每个子串均为回文串。
题目分析:可以用普通的区间dp做法,但是字符串的长度达到了4000,n^3会超时,要想办法降到n^2。想了很久,没有想出来,看了博客的做法。先预处理出来区间[i, j]是否为回文串。假如字符串的长度为10, 那么以6为长度的字符串的最少分割数就等于min(dp[1~5] && is[1~5]。好像说的不太清,看代码吧。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int maxn = 4e3 + 100;
const int inf = 0x3f3f3f3f;

int dp[maxn], ans[maxn];
bool is[maxn][maxn], vis[maxn];
char str[maxn];
//dp[i]表示从1~i的最小分割数

int main()
{
    scanf("%s", str + 1);
    int len = strlen(str + 1);

    for(int i = 1; i <= len; i++) {
        is[i][i] = true;
       int l = i - 1, r = i + 1;
       while(1 <= l && r <= len && str[l] == str[r]) {
          is[l][r] = true;
          l--; r++;
       }

       l = i; r = i + 1;
       while(1 <= l && r <= len && str[l] == str[r]) {
           is[l][r] = true;
           l--; r++;
       }
    }


    memset(dp, inf, sizeof(dp));
    memset(ans, 0, sizeof(ans));
    dp[0] = 0;

    for(int i = 1; i <= len; i++) { //枚举长度,因为6这个长度的结果是可以用5推过来的。
        for(int j = 1; j <= i; j++) { //枚举当前长度的断开点
            if(is[j][i] && dp[i] > dp[j - 1] + 1) {
                dp[i] = dp[j - 1] + 1;
                ans[i] = j - 1; //记录断开点
            }
        }
    }

    printf("%d\n", dp[len]);

    int pre = ans[len];
    while(pre) {
        vis[pre] = true;
        pre = ans[pre];
    }

    for(int i = 1; i <= len; i++)
    {
        printf("%c", str[i]);
        if(vis[i]) printf(" ");
    }
    printf("\n");

}

后来的时候,又思考了一下,为什么这个可以用一维来做,之前的取石子问题,括号匹配不能。我认为,是因为这个题dp[i]的状态可以由dp[i - 1]推过来,但是之前的问题都是不可以的。

有谁知道ural上的1028题如何解

01-16

有谁知道ural上的1028题如何解rnStars.rnTime Limit: 1 secondrnMemory Limit: 1000KrnrnAstronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.rnrn rnrnFor example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. rnrnYou are to write a program that will count the amounts of the stars of each level on a given map. rnInputrnThe first line of the input contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.rnOutputrnThe output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.rnSample Inputrn5rn1 1rn5 1rn7 1rn3 3rn5 5rnrnSample Outputrn1rn2rn1rn1rn0rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试