注册 登录
编程论坛 C++教室

大神帮忙看一看c++的程序:最大特征值

a344304180 发布于 2013-01-20 16:11, 548 次点击

Description:
大写字母A-Z分别对应整数[-13,12],因此,一个字符串对应了一个整数列。我们把字符串对应的整数列的和称为该字符串的特性值。例如:字符串ACM对应的整数列为{-13,-11,-1},则ACM的特性值为(-13)+(-11)+(-1)=-25;其子串AC的特性值为-24;子串M的特性值为-1。 给你一个字符串,请找出该字符串的所有子串的最大特性值。
 

Input:
第一行的正整数 N(1<=N<=1000)表示其后有N组测试数据,每组测试数据都由一个字符串组成。字符串只能由大写字母A-Z组成,且字符串的长度不会超过1000。
Output:
输出有N行,每行是一组测试数据的输出结果。
Sample Input:
2
ACM
ANGRY

 Sample Output:
-1
15

本人编写:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()

{
    int n,z=0;
    char t='A';
    vector<int>f;
    string a;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a;
        for(int j=0; j<a.size(); j++)
        {
            if(a[j]<='N')
            {
                if(a[j]>t)
                t=a[j];
            }
            else
            {
                z=j+1;
                break;
            }
        }
        if(z==0)
        cout<<t-78<<"\n";
        else
        {
            char temp,temp1,temp2='A'-78;
            z=z-1;
            for(;z<a.size();z++)
            {
                if(a[z]>'N')
                f.push_back(z);
            }
            for(int x=0; x<f.size(); x++)
            {
                for(int y=x+1; y<=f.size(); y++)
                {
                    int sum=0;
                    for(int w=f[x]+1; w<=f[y]; w++)
                        sum+=(a[w]-78);
                        if(sum>0)
                        {
                            temp1=a[f[x]]-78+sum;
                            if(temp1>temp2)
                                temp2=temp1;

                        }
                }
            }
            cout<<temp2-1+1<<"\n";
        }
    }
    return 0;
}
不能AC,问题出在哪里?更简洁的解法?还有最后字符ASCII码怎么表示??
4 回复
#2
qq5352632842013-01-20 18:12
加油
#3
milantgh2013-01-20 20:14
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()

{
    int n,z=0;
    char t='A';
    vector<int>f;
    string a;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a;
        for(int j=0; j<a.size(); j++)
        {
            if(a[j]<='N')
            {
                if(a[j]>t)
                t=a[j];
            }
            else
            {
                z=j+1;
                break;
            }
        }
        if(z==0)
        cout<<t-78<<"\n";
        else
        {
            char temp1,temp2='A'-78;
            z=z-1;
            for(;z<a.size();z++)
            {
                if(a[z]>'N')
                f.push_back(z);
            }
            for(int x=0; x<f.size(); x++)
            {
                for(int y=x+1; y<=f.size(); y++)
                {
                    int sum=0;
                    for(int w=f[x]+1; w<=f[y]; w++)
                        sum+=(a[w]-78);
                        if(sum>0)
                        {
                            temp1=a[f[x]]-78+sum;
                            if(temp1>temp2)
                                temp2=temp1;

                        }
                }
            }
            cout<<temp2-1+1<<"\n";
        }
    }
    return 0;
#4
milantgh2013-01-20 20:15
这样就可以了!!!
#5
pangding2013-01-21 17:07
貌似就是最大子串和问题吧,多了一个字母转数字的过程。
这里有解释:http://blog.
1