HDU 2577 DP || 贪心

71 篇文章 0 订阅
10 篇文章 1 订阅

DP:

#include "stdio.h"
#include "string.h"

int Min(int a,int b)
{
    if (a<b) return a;
    else return b;
}
int main()
{
    int n,i,le;
    int a[1001],b[1001];
    char str[1001];
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        while(n--)
        {
            gets(str);
            le=strlen(str);
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            if (str[0]>='a' && str[0]<='z')
            {
                a[0]=1;
                b[0]=2;
            }
            else
            {
                a[0]=2;
                b[0]=2;
            }
            for (i=1;i<le;i++)
            {
                if (str[i]<='Z' && str[i]>='A')
                {
                    a[i]=Min(a[i-1]+2,b[i-1]+2);
                    b[i]=Min(a[i-1]+2,b[i-1]+1);
                }
                else
                {
                    a[i]=Min(a[i-1]+1,b[i-1]+2);
                    b[i]=Min(a[i-1]+2,b[i-1]+2);
                }
            }
            b[le-1]++;
            if ( a[le-1]<b[le-1]) printf("%d\n",a[le-1]);
            else printf("%d\n",b[le-1]);
        }
    }
    return 0;
}


贪心:

#include "stdio.h"
#include "string.h"
int main()
{
    int le,n,ans,c,i;
    char str[101];
    while (scanf("%d",&n)!=EOF)
    {
        getchar();
        while (n--)
        {
            gets(str);
            le=strlen(str);
            ans=0;
            c=0;
            for (i=0;i<le-1;i++)
            {
                if (str[i]<='z' && str[i]>='a' && c==0) ans++;

                if (str[i]<='Z' && str[i]>='A' && c==1) ans++;

                if (str[i]<='z' && str[i]>='a' && c==1)
                {
                    if (str[i+1]<='z' && str[i+1]>='a')
                    {
                        ans+=2;
                        c=0;
                    }
                    else
                    {
                        ans+=2;
                    }
                }

                if (str[i]<='Z' && str[i]>='A' && c==0)
                {
                    if (str[i+1]<='Z' &&str[i+1]>='A')
                    {
                        ans+=2;
                        c=1;
                    }
                    else
                        ans+=2;

                }

            }
            if (str[le-1]<='Z' && str[le-2]>='A')
            {
                if (c==0) ans+=2;
                else ans+=2;
            }
            else
            {
                if (c==0) ans+=1;
                else ans+=2;
            }


            printf("%d\n",ans);

        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值