C++ Interesting卡常数

  1. IO优化
    • fread 和 fwrite ,如果还想再优化有mmap....(然而并不会用,好像也没用。。。)
    • 读入优化(这个非常重要!!!!!!!)
    inline int Read()
    {
        int x=0,f=1;char c=getchar();
        while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
        while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    • 输出优化好像用不到唉( ˇˍˇ )
  2. inline
    在声明函数之前写上inline修饰符(就像上面Read()一样),可以加快一下函数调用,但只能用于一些操作简单的函数。涉及递归,大号的循环等很复杂的函数,编译器会自动忽略inline。

  3. register
    在定义变量前写上register修饰符,用于把变量放到CPU寄存器中,适用于一些使用频繁的变量:
register int n,m;

寄存器空间有限,如果放得变量太多,多余变量就会被放到一般内存中;
快,不是一般的快,快到什么程度呢?:

    register int a=0;
    for(register int i=1;i<=999999999;i++)
    a++;
    int a=0;
    for(int i=1;i<=999999999;i++)
    a++;

结果:
优化:0.2826 second
不优化:1.944 second
恐怖啊!!!!

  1. 循环展开

    循环展开也许只是表面,在缓存和寄存器允许的情况下一条语句内大量的展开运算会刺激 CPU 并发(前提是你的 CPU 不是某 CPU)...

  2. 取模优化(仅O2)
//设模数为 mod
inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}//代替取模+
inline int dec(int x,int v,int mod){x-=v;return x<0?x+mod:x;}//代替取模-
  1. 前置 ++

    后置 ++ 需要保存临时变量以返回之前的值,在 STL 中非常慢。事实上,int 的后置 ++ 在实测中也比前置 ++ 慢 0.5 倍左右(UOJ 上自定义测试)

  2. 不要开bool,所有bool改成char,int是最快的(原因不明)。

  3. if()else语句比()?():()语句要慢,逗号运算符比分号运算符要快。

  4. 数据结构用指针代替数组(个人觉得无关紧要)

    数组在用方括号时做了一次加法才能取地址!
    所以在那些计算量超大的数据结构中,你每次都多做了一次加法!!!在 64 位系统下是   long long 相加,效率可想而知。

转自该大佬的博客:http://www.cnblogs.com/widerg/p/7353866.html 

7-15 Words Fascinating 分数 35 作者 罗中天 单位 中国计量大学 As we all know the fact, you can use several Source Words to compose an Interesting Word. Little Gyro has already learnt this fantastic idea from one of his best friends Brother Yu. On Little Gryo's birthday, fortunately, Little Gryo received a gift from Brother Yu. The gift consists of n Interesting Words S 1 ​ ,S 2 ​ ,...,S n ​ . Little Gyro also thought those words were really fantastic. So he decided to play with these words. For each Interesting Word S i ​ , Little Gyro will select a period of consecutive letters P i ​ , and splice into a new word T within the given order, and then he defined these kind of words "Fascinating Words". It means you can take apart a Fascinating Word T and form n period of consecutive letters P 1 ​ +P 2 ​ +...+P n ​ from the given Interesting Words. Specially, if Little Gyro considers the Interesting Word really useless, he'll not choose any letter from this Interesting Word either. For example, supposed that there are some Interesting Words: S 1 ​ ="telephone", S 2 ​ ="microscope". Little Gyro may select a period of consecutive letters from each given word such as: P 1 ​ ="tele", P 2 ​ ="scope". And then form the Fascinating Word T=P 1 ​ +P 2 ​ ="telescope". Specially, Little Gyro also can only select P 2 ​ ="scope" and form the Fascinating Word T=P 1 ​ +P 2 ​ ="scope", if he dislikes the first Interesting Word. Now given all the Interesting Words that Little Gyro has received on his birthday, Little Gyro wants to know the total number of the different Fascinating Words that he can generate. Input Specification: Each input file only contains one test case. The first line contains an integer n (1 ≤ n ≤ 2000), indicating the number of the Interesting Words. Then, the following n lines, each line contains an Interesting Word S i ​ (1 ≤ ∑ i=1 n ​ ∣S i ​ ∣ ≤ 5×10 4 ). It's guaranteed that the Interesting Words can only be made up by the lowercase letters, and the sum of the length of the Interesting Words S i ​ of all test cases will not exceed 5×10 4 . Output Specification: For each test case, output the number of the different Fascinating Words that Little Gyro can generate. Because the number may be very large, just output the number mod 10 9 +7. Sample Input 1: 2 aa ab Sample Output 1: 8 Sample Input 2: 3 abb aca aba Sample Output 2: 139 Hint: For the first sample, Little Gyro can generate 8 different Fascinating Words in total, including "a", "b", "aa", "ab", "aaa", "aab", "aaab" and the empty word. 代码长度限制 16 KB Java (javac) 时间限制 2000 ms 内存限制 64 MB 其他编译器 时间限制 1000 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
10-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值