这题算是比较简单吧,dp[i][0]表示non-caps,dp[i][1]表示caps
状态转移方程还是比较简单的,注意小写的时候 caps+shift = lower
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <stack> #include <queue> #include <string> const int inf = (1<<31); const int MAXN = 1e5+10; using namespace std; int a[MAXN][2]; int main() { int t; scanf("%d",&t); string s; while(t--){ cin>>s; int n = s.length(); int mmin = inf; //cout<<n<<endl; a[0][1] = 1; a[0][0] = 0; for(int i=1;i<=n;i++){ if(s[i-1]<=‘Z‘&&s[i-1]>=‘A‘){ a[i][0] = min(a[i-1][0]+2,a[i-1][1]+2); a[i][1] = min(a[i-1][0]+2,a[i-1][1]+1); } else if(s[i-1]<=‘z‘&&s[i-1]>=‘a‘){ a[i][0] = min(a[i-1][0]+1,a[i-1][1]+2); a[i][1] = min(a[i-1][0]+2,a[i-1][1]+2); //caps+shift = lower } } mmin = min(a[n][1]+1,a[n][0]); cout<<mmin<<endl; } return 0; }
时间: 2024-11-01 01:38:49