uva 10029 简单DP

                两个for循环 2.632s水过。。。。。。。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#define MAXN 25000+5
using namespace std;
typedef struct node
{
    char str[20];
    int len;
} node ;
node word[MAXN];
bool suit(int x,int y)
{
    if(word[x].len==word[y].len)
    {
        int flag=0;
        for(int i=0,j=0; word[x].str[i]!='\0'; i++,j++)
        {
            if(word[x].str[i]!=word[y].str[j])
            {
                if(flag)   return false;
                else flag=1;
            }
        }
    }
    else if(word[x].len+1==word[y].len)
    {
        int flag=0;
        for(int i=0,j=0; word[y].str[i]!='\0'; i++)
        {
            if(word[y].str[i]!=word[x].str[j])
            {
                if(flag)   return false;
                else flag=1;
                continue;
            }
            j++;
        }
    }
    else if(word[x].len-1==word[y].len)
    {
        int flag=0;
        for(int i=0,j=0; word[x].str[i]!='\0'; i++)
        {
            if(word[x].str[i]!=word[y].str[j])
            {
                if(flag)   return false;
                else flag=1;
                continue;
            }
            j++;
        }
    }
    else return false;
    return true;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int d[MAXN],cnt=0;;
    while(scanf("%s",word[cnt].str)!=EOF)
    {
        word[cnt].len=strlen(word[cnt].str);
        cnt++;
    }
    int maxx=0;
    for(int i=0; i<cnt; i++)
    {
        d[i]=1;
        for(int j=0; j<i; j++)
        {
            if(suit(i,j))
            {
                d[i]=max(d[i],d[j]+1);
                maxx=max(maxx,d[i]);
            }
        }
    }
    printf("%d\n",maxx);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值