sequence

时间:2024-10-13 07:10:28

要求对一个字符串分割,使得他上升

就是dp

比较两个字符串的大小,然后就根据转移

代码:

#include <bits/stdc++.h>
using namespace std;
//i:n~1 j:n-i+1~1
const int N = 5010,mod = 10000007;
int f[N][N];
int a[N][N];
string s;
int checker(int i,int j){
	int t = min(j - 1,a[i][i + j]);
	return s[i + t] < s[i + j + t];
}
int main(){
	int n;
	cin >> n;
	cin >> s;
	s = ' ' + s;
	for (int i = n; i >= 1; i -- ){
		for (int j = n; j >= 1; j -- ){
			if (s[i] == s[j]){
				a[i][j] = a[i + 1][j + 1] + 1;
			}
		}
	}
	for (int i = n; i >= 1; i -- ){
		for (int j = n - i; j >= 1; j -- ){
			if (s[i] == '0'){
				continue;
			}
			int t = 0;
			f[i][n - i + 1] = 1;
			if (checker(i,j)){
				f[i][j] = (f[i][j] + f[i + j][j]) % mod; 
			} 
			else{
				f[i][j] = (f[i][j] + f[i + j][j + 1]) % mod;
			}
		}
		for (int j = n; j >= 1 ; j --){
			f[i][j] = (f[i][j] + f[i][j + 1]) % mod;
		}
	}
	cout << f[1][1];
	return 0;
}