hdu-1003-Max Sum

来源:互联网 发布:nginx 跳转到二级目录 编辑:程序博客网 时间:2024/05/29 12:14

思路:

最长子段和变形,只是要注意下有负数解

#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>using namespace std;char s[500500];int table[100];int solve(int tx){    int i;    for( i=tx;i<tx+26;i++)    {        if(table[s[i]-'A']!=0&&s[i]!='?')        {            return -1;        }        else        {            if(s[i]!='?')            table[s[i]-'A']=1;        }    }    if(i==tx+26)    {        for(int j=tx;j<tx+26;j++)        {            if(s[j]=='?')            {                int t;                for(int k=0;k<=25;k++)                {                    if(table[k]==0)                        {table[k]=1;                         t=k;                         break;                        }                }                s[j]='A'+t;            }        }    }    return 1;}int main(){    scanf("%s",&s);    if(strlen(s)<26)    {        cout<<-1<<endl;        return 0;    }    int i;    for( i=0;i<=strlen(s)-26;i++)    {        memset(table,0,sizeof(table));        int tmp=solve(i);        if(tmp==1)            break;    }    if(i==strlen(s)-25)        cout<<-1<<endl;    else    {        for(int i=0;i<=strlen(s);i++)        {            if(s[i]=='?')                s[i]='A';        }        puts(s);    }    return 0;}


0 0
原创粉丝点击