gpt4 book ai didi

c++ - SPOJ "CENCRY"代码gettingTLE

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:42 26 4
gpt4 key购买 nike

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;
}
}

最佳答案

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

顺便说一句。你应该给我们链接到你的任务,因为你的算法可能根本就错了,即使我以前的建议也不足以解决你的问题。

关于c++ - SPOJ "CENCRY"代码gettingTLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20737347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com