洛谷P2031 脑力达人之分割字串

洛谷P2031 脑力达人之分割字串 

f[ i ] 表示前 i 个字符最多能分割成几份

从第 i 位 枚举 模式串 枚举他们是否能够匹配
能就取 max

 

 

 1 #include <bits/stdc++.h>  
 2 #define For(i,j,k) for(int i=j;i<=k;i++) 
 3 using namespace std ; 
 4 
 5 const int N = 311 ; 
 6 char s[N] ; 
 7 int n,L,mx,l ; 
 8 char type[501][N] ; 
 9 int f[N],len[511] ; 
10 bool flag ; 
11 
12 inline int read() 
13 {
14     int x = 0 , f = 1 ; 
15     char ch = getchar() ; 
16     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; } 
17     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar() ; } 
18     return x * f ;  
19 }
20 
21 int main() 
22 {
23     scanf("%s",s+1) ; 
24     n = read() ; 
25     L = strlen(s+1) ; 
26     For(i,1,n) scanf("%s",type[ i ]+1) ; 
27     f[ 0 ] = 0 ; 
28     For(i,1,L) { 
29         f[ i ] = mx ; 
30         For(j,1,n) {
31             l = strlen(type[ j ]+1) ; 
32             if( i < l ) continue ; 
33             flag = 0 ; 
34             For(k,1,l) if( s[ i-l+k ]!=type[ j ][ k ] ) {
35                 flag = 1 ; 
36                 break ; 
37             }
38             if(flag) continue ; 
39             f[ i ] = max( f[ i-l ]+1,f[ i ] ) ; 
40         }
41         if( f[ i ] > mx ) mx = f[ i ] ; 
42     }
43     //f[ L ] = mx ;  
44     printf("%d\n",f[ L ]) ; 
45     return 0 ; 
46 }
47 
48 

 

posted @ 2017-08-31 13:24  third2333  阅读(202)  评论(0编辑  收藏  举报