dp[ i ] 表示前 i 个字符的最优解。
if( s[i] 为 latin letter || space)
{
if(如果前 (i-1) 个字符的后 m 个 全部为latin letter || space)
dp[ i ] = min(dp [i-k]) + 1;
//(1 <= k &&k <= m);
else
dp[ i ] = min( min(dp[ site + k1 ]) , min(dp[i-k2]) )+1;
//(site为最右面的一个 !(latin letter || space),site+k1 <= i-1,1 <= k2 && k2 <= n);
}
else
{
dp[i] = min ( dp[ i-k ] ) + 1;
//(1 <= k && k <= n);
}
数据范围较大,需要用数据结构优化。愈发懒了. . . . . .
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long int
#define ULL unsigned long long int
#define _LL __int64
#define _INF 0x3f3f3f3f
#define INF 4000000
#define Mod 1000000009
using namespace std;
char s[100001];
int dp[100001];
int st[400004];
void Init(int site,int l,int r)
{
if(l == r)
{
if( s[l] == ' ' || ('A' <= s[l] && s[l] <= 'Z') || ('a' <= s[l] && s[l] <= 'z') )
{
st[site] = -1;
}
else
{
st[site] = l;
}
return ;
}
int mid = (l+r)>>1;
Init(site<<1,l,mid);
Init(site<<1|1,mid+1,r);
st[site] = max(st[site<<1],st[site<<1|1]);
}
int Query(int site,int l,int r,int L,int R)
{
if(L == l && R == r)
{
return st[site];
}
if(st[site] == -1)
return -1;
int mid = (L+R)>>1;
if(r <= mid)
{
return Query(site<<1,l,r,L,mid);
}
if(mid < l)
{
return Query(site<<1|1,l,r,mid+1,R);
}
return max(Query(site<<1,l,mid,L,mid),Query(site<<1|1,mid+1,r,mid+1,R));
}
int st1[400100];
void Init_Min(int site,int l,int r)
{
if(l == r)
{
st1[site] = INF;
return ;
}
int mid = (l+r)>>1;
Init_Min(site<<1,l,mid);
Init_Min(site<<1|1,mid+1,r);
st1[site] = INF;
}
void Updata(int site,int l,int r,int m,int x)
{
if(l == r && r == m)
{
st1[site] = x;
return ;
}
int mid = (l+r)>>1;
if(m <= mid)
{
Updata(site<<1,l,mid,m,x);
}
else
{
Updata(site<<1|1,mid+1,r,m,x);
}
st1[site] = min(st1[site<<1],st1[site<<1|1]);
}
int Query1(int site,int l,int r,int L,int R)
{
if(l == L && R == r)
{
return st1[site];
}
int mid = (L+R)>>1;
if(r <= mid)
{
return Query1(site<<1,l,r,L,mid);
}
else if(mid < l)
{
return Query1(site<<1|1,l,r,mid+1,R);
}
return min(Query1(site<<1,l,mid,L,mid),Query1(site<<1|1,mid+1,r,mid+1,R));
}
int main()
{
int n,m,i,l,temp;
scanf("%d %d%*c",&n,&m);
gets(s+2);
l = strlen(s+2)+1;
s[0] = ' ';
s[1] = ' ';
dp[0] = 0;
dp[1] = 0;
Init_Min(1,1,l);
Updata(1,1,l,1,0);
//cout<<"l = "<<l<<endl;
Init(1,1,l);
for(i = 2;i <= l; ++i)
{
if( s[i] == ' ' || ('A' <= s[i] && s[i] <= 'Z') || ('a' <= s[i] && s[i] <= 'z') )
{
temp = Query(1,max(1,i-m),i-1,1,l);
if(temp == -1)
{
dp[i] = Query1(1,max(1,i-m),i-1,1,l)+1;
}
else
{
dp[i] = Query1(1,temp,i-1,1,l)+1;
dp[i] = min(dp[i],Query1(1,max(1,i-n),i-1,1,l) + 1);
}
}
else
{
dp[i] = Query1(1,max(1,i-n),i-1,1,l) + 1;
}
Updata(1,1,l,i,dp[i]);
}
/*for(i = 1;i <= l; ++i)
{
printf("i = %2d dp = %2d s = %c\n",i,dp[i],s[i]);
}*/
printf("%d\n",dp[l]);
return 0;
}