一些技巧
卡常基础
- 码风不够优秀?卡常!
常用卡常方法:
- 用
"\n"替换掉你的endl cin,cout时最好关闭同步流(当然,还不行就只能上scanf,printf了,它们还是要快一些)- 非递归函数前可以加上
inline,但有时候会变得更慢。。。
神奇的Hash
- 对于字符串 Hash,要么就手写,要么就上 map
此处给出一种针对 map<srting, int> 类型的时间优化方案:记录字符串的 Hash 值用于计数,而非直接扔字符串。实现:
// 字符串哈希函数
size_t string_hash(const string& s) {
return hash<string>{}(s);
}
unordered_map<size_t, int> cnt[MAXN]; // 哈希值->计数
set<size_t> total[MAXN]; // 存在的哈希值集合
实测这种方法可以优化不少!3000+ ms=>1404 ms