SPOJ“CENCRY”代码getTLE

时间:2013-12-23 05:15:17

标签: c++ time

CENCRY

在spoj中描述的问题,这里是我的源代码,我的源代码有订单O(strlen),但仍然超出时间限制,在逻辑方面有任何帮助。

int main()
{
    int T,pos,loc;
    cin>>T;
    char str[50000], vowel[]= {"aeiou"}, consonant[] = {"bcdfghjklmnpqrstvwxyz"};
    int hash[26]={0,0,1,2,1,3,4,5,2,6,7,8,9,10,3,11,12,13,14,15,4,16,17,18,19,20};

    while(T--)
    {
    int hashC[26]={0};
    cin >> str;
    for(int i = 0;i < strlen(str); i++)
    {
        int ch=str[i]-97;
        pos = hash[ch];
        loc = hashC[ch];
        switch(ch)
        {
        case 0:
        case 4:
        case 8:
        case 14:
        case 20:
            cout<<consonant[(loc*5+pos)%21];
            break;
        default:
            cout<<vowel[(loc*21+pos)%5];
            break;
        }
        hashC[ch]++;
    }
    cout<<endl;
    }
}

1 个答案:

答案 0 :(得分:0)

而不是使用std :: cin和std :: cout尝试使用scanf和printf - 这些函数可能会使程序在stdin上给出大量数据时更快。 除了... 您应该使用srlen ONCE并将输出写入某个变量,并在“for”循环中使用它。

顺便说一句。 你应该给我们链接到你的任务,因为你的算法可能是错误的,甚至我以前的建议也无法解决你的问题。