题意不太好理解 其它没什么 细心啊 细心 一个0写成了1 WA半天
以每个字符是以第一种方式还是第二种方式来D
1 #include <iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<stdlib.h> 5 #include<cstring> 6 #include<vector> 7 using namespace std; 8 #define N 100010 9 char s[N]; 10 int dp[N][2],o[N][2]; 11 int judge(char x) 12 { 13 if(x==' ') 14 return 1; 15 if(x>='a'&&x<='z') 16 return 1; 17 if(x>='A'&&x<='Z') 18 return 1; 19 return 0; 20 } 21 int main() 22 { 23 int n,m,i,k=0; 24 scanf("%d%d%*c",&n,&m); 25 gets(s); 26 k = strlen(s); 27 dp[0][1] = 1; 28 o[0][1] = 1; 29 if(judge(s[0])) 30 { 31 dp[0][0] = 1; 32 o[0][0] = 1; 33 } 34 else 35 { 36 dp[0][0] = -1; 37 o[0][0] = 0; 38 } 39 for(i = 1 ; i < k ; i++) 40 { 41 if(judge(s[i])) 42 { 43 if(o[i-1][0]>=m) 44 { 45 dp[i][0] = min(dp[i-1][1]+1,dp[i-1][0]+1); 46 o[i][0] = 1; 47 } 48 else 49 { 50 if(o[i-1][0]&&dp[i-1][0]<dp[i-1][1]+1) 51 { 52 dp[i][0] = dp[i-1][0]; 53 o[i][0] = o[i-1][0]+1; 54 } 55 else 56 { 57 dp[i][0] = dp[i-1][1]+1; 58 o[i][0] = 1; 59 } 60 } 61 } 62 else 63 o[i][0] = 0; 64 if(o[i-1][1]>=n) 65 { 66 if(o[i-1][0]) 67 dp[i][1] = min(dp[i-1][1]+1,dp[i-1][0]+1); 68 else 69 dp[i][1] = dp[i-1][1]+1; 70 o[i][1] = 1; 71 } 72 else 73 { 74 if(!o[i-1][0]||dp[i-1][1]<dp[i-1][0]+1) 75 { 76 dp[i][1] = dp[i-1][1]; 77 o[i][1] = o[i-1][1]+1; 78 } 79 else 80 { 81 dp[i][1] = dp[i-1][0]+1; 82 o[i][1] = 1; 83 } 84 } 85 86 } 87 if(o[k-1][0]!=0) 88 printf("%d ",min(dp[k-1][0],dp[k-1][1])); 89 else 90 printf("%d ",dp[k-1][1]); 91 return 0; 92 }