Girls Love 233

Girls Love 233

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
Besides skipping class, it is also important to meet other girls for luras in the new term.

As you see, luras sneaked into another girl's QQgroup to meet her indescribable aim.

However, luras can only speak like a cat. To hide her real identity, luras is very careful to each of her words.

She knows that many girls love saying "233",however she has already made her own word at first, so she needs to fix it.

Her words is a string of length n,and each character of the string is either '2' or '3'.

Luras has a very limited IQ which is only m.

She could swap two adjacent characters in each operation, which makes her losing 2 IQ.

Now the question is, how many substring "233"s can she make in the string while her IQ will not be lower than 0 after her operations?

for example, there is 1 "233" in "2333", there are 2 "233"s in "2332233", and there is no "233" in "232323".
 
Input
The first line is an integer T which indicates the case number.

and as for each case,

the first line are two integers n and m,which are the length of the string and the IQ of luras correspondingly.

the second line is a string which is the words luras wants to say.

It is guaranteed that——

1 <= T <= 1000

for 99% cases, 1 <= n <= 10, 0 <= m <= 20

for 100% cases, 1 <= n <= 100, 0<= m <= 100
 
Output
As for each case, you need to output a single line.

there should be one integer in the line which represents the largest possible number of "233" of the string after her swap.
 
Sample Input
3 6 2 233323 6 1 233323 7 4 2223333
 
Sample Output
2 1 2
分析:首先想到只会交换相邻的2和3,那么2的相对位置不变;
   考虑dp一下2的所有放置情况;
   dp[i][j][k]表示第几个2,放的位置,当前剩余步数下所得到的233;
   则转移时只需考虑上一个2的位置即可,如果与上一个2坐标差>2,则转移时+1;
   (还好有数据,orz~)
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
const int N=1e3+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,dp[110][110][51],pos[110],cnt,cas,ret;
char a[110];
int main()
{
    int i,j;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        m/=2;
        ret=cnt=0;
        scanf("%s",a+1);
        for(i=1;i<=n;i++)if(a[i]=='2')pos[++cnt]=i;
        pos[++cnt]=i;
        memset(dp[0],-1,sizeof(dp[0]));
        dp[0][0][m]=0;
        for(i=1;i<=cnt;i++)
        {
            for(j=i;j<=n+1;j++)
            {
                for(k=0;k<=m;k++)
                {
                    dp[i][j][k]=-1;
                    for(t=i-1;t<j;t++)
                    {
                        if(k+abs(j-pos[i])<=m&&dp[i-1][t][k+abs(j-pos[i])]!=-1)
                        {
                            dp[i][j][k]=max(dp[i][j][k],dp[i-1][t][k+abs(j-pos[i])]+(j-t>2&&i>1));
                        }
                    }
                    if(i==cnt&&j==n+1&&ret<dp[i][j][k])ret=dp[i][j][k];
                }
            }
        }
        printf("%d\n",ret);
    }
    return 0;
}
/*
1
5 19
33233
ans:1
*/
posted @ 2017-02-26 00:45  mxzf0213  阅读(626)  评论(0编辑  收藏  举报