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

时间:2021-07-15 18:39:36

题目传送门

解题思路:

f[i]表示到第i位可获得的最大分割次数,对于每个f[i]都可由其符合条件的前缀转移过来,条件就是当前串除了前缀的剩余字符里有所给单词,然后一看,这不是在剩余字符里找有没有所给单词吗?所以果断KMP,其实本题好像不用KMP,暴力模拟就可以,但是为了练习KMP装逼,所以就写一下.

AC代码:

 #include<iostream>
#include<cstdio> using namespace std; string l,p,a[];
int n,kmp[][],f[]; inline int max(int s,int d) {
if(s > d) return s;
return d;
} int main() {
cin >> p;
l = " " + p;
scanf("%d",&n);
for(int i = ;i <= n; i++) {
cin >> p;
a[i] = " " + p;
int len = a[i].length() - ,k = ;
for(int j = ;j <= len; j++) {
while(k != && a[i][j] != a[i][k+]) k = kmp[i][k];
if(a[i][j] == a[i][k+]) k++;
kmp[i][j] = k;
}
}
for(int i = ;i <= l.length() - ; i++) {
for(int j = ;j <= i; j++) {
string u = " " + l.substr(j,i-j+);
int o;
bool vis = ;
for(int k = ;k <= n; k++) {
o = ;
for(int p = ;p <= u.length() - ; p++) {
while(o != && u[p] != a[k][o+]) o = kmp[k][o];
if(u[p] == a[k][o+]) o++;
if(o == a[k].length() - ) {
vis = ;
break;
}
}
if(vis) break;
}
if(vis)
f[i] = max(f[i],f[j-] + );
}
}
printf("%d",f[l.length()-]);
return ;
}