【错题本】刷算法题中出现的一些错误和小技巧

想一想还是弄一个错题本吧,acm和c++的错误统一记录

2018-02-08 01:13

  • HDU-1263 水果
    不要再习惯用cin和string做搭配
    不要企图把char* 作为stl::map的键了(虽然可行,附上代码,以后也别用)
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
struct cmp{
    bool operator() (const char *a, const char *b) const{
        return strcmp(a, b);
    }
};
map<char*, char*, cmp> dict;

int main(void){
    // 注意必须新分配一个地址,不然会覆盖原值
    // 同时注意delete[]内存,综合考虑可能需要维护一个内存池
    char *stra=new char[20];
    char *strb=new char[20];
    scanf("%s%s", stra, strb);

    dict[stra]=strb;
    printf("%s
", dict["haha"]);

    return 0;
}
// 输入 haha nimeia
// 输出 nimeia
  • POJ-2833 The Average
    记不住单词priority(拍哦若忒),优先队列默认权值大的在队头
priority_queue<int> low;
priority_queue<int, vector<int>, greater<int> > high;
// 需要头文件 functional


>2018-02-10 01:05 - [HDU-2303 The Embarrassed Cryptographe 高精度算法(大数取模)](http://www.cnblogs.com/tanglizi/p/8437105.html) 大数取模,其实就是一位一位模拟除法,脑子秀逗了一开始想半天
char a[255];
inline int mod(const int &idx, const int &length){
    int ans=0;
    for (int i=0; i<length; i++)
        ans=(ans*10+a[i]-'0')%primes[idx];
    return ans;
}
BigInteger a=new BigInteger("123");
BigInteger d = new BigInteger("3", 8);      //n进制字符串 转 BigInteger 
a=BigInteger.valueOf(10);                        //int 转 BigInteger
a.add(b);
a.subtract(b);
a.multiply(b);
a.divide(b);
a.mod(b);                //取模a%b,b需大于0,5mod3=2 -5mod3=1
 
a.abs();
a.equals(b);            // a==b
a.signum();             //正为1 0为0 负为-1
a.compareTo(b);    //比较a>b返回1 a==b返回0 a<b返回-1
a.max(b); 
a.min(b); 
a.pow(n);


>2018-03-21 00:04 - [ZOJ-3261 Connections in Galaxy War 并查集 离线操作](http://www.cnblogs.com/tanglizi/p/8613951.html) 使用**离线操作**可以将大部分操作**反向处理**(若有正向反向共存,不要直接用这个) 如并查集的删边(不能同时合并)

>2018-03-21 00:04 - 关于**并查集根节点的维护**: - [ZOJ-3261 Connections in Galaxy War 并查集 离线操作](http://www.cnblogs.com/tanglizi/p/8613951.html) 维护**根节点为一最值**
原文地址:https://www.cnblogs.com/tanglizi/p/8429272.html