zoukankan      html  css  js  c++  java
  • P2031 脑力达人之分割字串

    P2031 脑力达人之分割字串
    字符串dp,f[i]表示主串到第i个字符,最多能分割成多少子串。
    f[i]=max(f[i],f[k]+1);k是能匹配到的前一位。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<ctime>
     7 #include<set>
     8 #include<map>
     9 #include<stack>
    10 #include<cstring>
    11 #define inf 2147483647
    12 #define For(i,a,b) for(register int i=a;i<=b;i++)
    13 #define p(a) putchar(a)
    14 #define g() getchar()
    15 //by war
    16 //2017.10.29
    17 using namespace std;
    18 char a[310],c[521][310];
    19 int n;
    20 int la,lb;
    21 bool flag;
    22 int f[310];
    23 int mx;
    24 void in(int &x)
    25 {
    26     int y=1;
    27     char c=g();x=0;
    28     while(c<'0'||c>'9')
    29     {
    30     if(c=='-')
    31     y=-1;
    32     c=g();
    33     }
    34     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
    35     x*=y;
    36 }
    37 void o(int x)
    38 {
    39     if(x<0)
    40     {
    41         p('-');
    42         x=-x;
    43     }
    44     if(x>9)o(x/10);
    45     p(x%10+'0');
    46 }
    47 int main()
    48 {
    49     cin>>(a+1);
    50     la=strlen(a+1);
    51     in(n);
    52     For(i,1,n)
    53     cin>>(c[i]+1);
    54     For(i,1,la)
    55     {
    56         f[i]=mx;
    57         For(j,1,n)
    58         {
    59             flag=false;
    60             lb=strlen(c[j]+1);
    61             if(lb<=i)
    62             {
    63             For(k,1,lb)
    64             if(a[i-lb+k]!=c[j][k])
    65             {
    66                 flag=true;
    67                 break;
    68             }
    69             if(!flag)
    70             f[i]=max(f[i],f[i-lb]+1);
    71             }
    72         }
    73         if(f[i]>mx)
    74         mx=f[i];
    75     }
    76     o(f[la]);
    77      return 0;
    78 }
  • 相关阅读:
    UVA 10905
    UVA 10859 树形DP
    LA 4794 状态DP+子集枚举
    LA 3695 部分枚举
    UVA 11825 状态压缩DP+子集思想
    UVA 10891 区间DP+博弈思想
    HDU 5239 上海大都会 D题(线段树+数论)
    HDU 5242 上海大都会 G题
    HDU 5241 上海大都会 F题
    P1359 租用游艇
  • 原文地址:https://www.cnblogs.com/war1111/p/7749412.html
Copyright © 2011-2022 走看看