字符串基本题目,死得好懵。c++,求助,谢谢谢谢!!!

学术版

题目好像洛谷没有。这是某省模拟赛的题目路。 @[AuCloud](/user/75954) 原谅我贸然向您求助。
by lmrttx @ 2020-10-18 14:50:49


洛谷不允许使用 `gets()`。
by LucasXu80 @ 2020-10-18 14:54:13


@[lmrttx](/user/344382) 由于 strlen() 的复杂度为 $O(|s|)$,所以您这么写的复杂度为 $O(|s|^2)$,会超时
by _5011_ @ 2020-10-18 14:56:56


@[lmrttx](/user/344382) - 用getline(cin,ch)读入字符串ch - 将所有字母转为小写 - 数组a注意初始化为0 - 先转小写,不要边转边计数
by Accepted喵 @ 2020-10-18 15:01:53


@[lmrttx](/user/344382) 还有就是用:int changdu=ch.size();获取字符串ch的长度
by Accepted喵 @ 2020-10-18 15:03:00


@[Zephyr_](/user/91127) 所以说,大佬,要怎么样才不会超时呢??? 可能我把strlen的复杂度记错了。。。 超时的话,评测机会显示WA吗??? (不是洛谷,不是洛谷)。
by lmrttx @ 2020-10-18 15:03:32


@ Accepted 谢谢您!!!我试试
by lmrttx @ 2020-10-18 15:04:48


@[lmrttx](/user/344382) 应该不会 WA:Wrong Answer TLE:超时
by Accepted喵 @ 2020-10-18 15:09:20


@[Accepted喵](/user/314240) char 数组貌似不能这么用吧…… 要 string 才行……
by Eason_AC @ 2020-10-18 15:14:44


我改成了这样,结果20分了。 ```cpp #include<bits/stdc++.h> using namespace std; int a[100001]; int main() { //freopen("edit.in","r",stdin); //freopen("edit.out","w",stdout); string s; getline(cin,s); transform(s.begin(),s.end(),s.begin(),::tolower); for(register int i=0;i<s.size();i++) { a[s[i]-'a']++; } for(register int i=0;i<26;i++) printf("%d ",a[i]); return 0; } ``` 有人帮帮我吗
by lmrttx @ 2020-10-18 15:20:55


| 下一页