ACdream 1116 Gao the string! (扩展KMP+矩阵快速幂)
来源:互联网 发布:写sql算研发么 编辑:程序博客网 时间:2024/06/04 04:54
题目链接:
ACdream 1116
题意:
give you a string, please output the result of the following function mod
(the prefix means
比如 :输入:
1.对于
2.对于
所以答案是
题解:
我们用
求最长公共前缀
AC代码:
#include<cstdio>#include<cstring>#include<iostream>//using namespace std;const int mod = 1e9+7;struct matrix{ long long factor[2][2];}M;matrix multi(matrix x,matrix y){ matrix temp; temp.factor[0][0] = (x.factor[0][0] * y.factor[0][0] + x.factor[0][1] * y.factor[1][0]) % 1000000007; temp.factor[0][1] = (x.factor[0][0] * y.factor[0][1] + x.factor[0][1] * y.factor[1][1]) % 1000000007; temp.factor[1][0] = (x.factor[1][0] * y.factor[0][0] + x.factor[1][1] * y.factor[1][0]) % 1000000007; temp.factor[1][1] = (x.factor[1][0] * y.factor[0][1] + x.factor[1][1] * y.factor[1][1]) % 1000000007; return temp;}matrix qpower(int n){ if(n == 1)return M; matrix tmp = qpower(n>>1); if(n&1){ return multi(multi(tmp,tmp),M); } else return multi(tmp,tmp);}int fib(int num){ M.factor[0][0] = 0; M.factor[0][1] = 1; M.factor[1][0] = 1; M.factor[1][1] = 1; int n = num -1; if(n > 0){ matrix tmp =qpower(n); return tmp.factor[1][1]; } else if(n == -1)return 0; else if(n == 0) return 1;}int min(int a,int b){ return a > b ? b : a; }int next[100010];void ex_kmp(char *t,int len){ next[0] = len - 1; int j = 0; while( 1 + j < len && t[j] == t[1 + j]) j++; next[1] = j - 1; int id = 1; for(int i = 2;i < len; i++) { int p = next[id] + id; int l = next [i - id]; if(p < i)next[i] = -1; else next[i] = min(l, p - i); while(next[i] + i + 1 < len && t[next[i] + i + 1]== t[next[i] + 1]){ next[i] ++; } if(i+next[i] > id + next[id]){ id = i; } } }char T[100010];int main(){ while(~scanf("%s",T)) { int len = strlen(T); ex_kmp(T,len); // for(int i=0;i<5;i++)std::cout<<next[i]<<" ";std::cout<<std::endl; int sum = 0,ans = 0; for(int i=len-1;i>=0;--i) { sum += next[i] + 1; ans = (ans + fib(sum))%mod; // std::cout<<"ans="<<ans<<std::endl; } printf("%d\n",ans); } return 0;}
阅读全文
1 0
- ACdream 1116 Gao the string! (扩展KMP+矩阵快速幂)
- Acdream 1116 Gao the string!(exkmp+矩阵快速幂)
- acdream 1116 Gao the string! (扩展kmp,dp思想,矩阵优化)
- acdream 1116 Gao the string!
- ACdream 字符串专题A Gao the string! EXKMP+矩阵快速幂
- Acdreamoj1116(Gao the string!)字符串hash+二分+矩阵快速幂
- 扩展KMP+矩阵快速幂
- acdream 1214 矩阵快速幂
- ACdream 1116 扩展KMP 线段树区间累加 Fib数列的矩阵加速
- ZOJ3533-Gao the String I
- acdream 1060 递推数 (矩阵快速幂+循环节)
- ACdream 1060 递推数 (矩阵快速幂+找循环节)
- hdu 3336 Count the string(扩展KMP模板)
- hdu 3336 Count the string (kmp扩展)
- Count the string (HDU_3336) 扩展KMP + dp
- ZOJ 3535 Gao the String II
- zoj3533 Gao the String I(splay)
- zoj 3647 Gao the Grid(矩阵对角线三点共线)
- 转义字符
- java SE 注意点 1
- 数组、链表、堆栈和队列
- 多重集组合数(动态规划(DP))
- 最大公约数欧几里得算法
- ACdream 1116 Gao the string! (扩展KMP+矩阵快速幂)
- hdu 2069 二维母函数
- ReactNative基础---Layout and Flexbox
- unity 制作动态适应的scrollview的注意点
- [转]Webview支持缩放并隐藏讨厌的缩放控制条
- 【Android源码分享】小k英语口袋
- adb devices报错总结
- malloc()和calloc()简介
- CSS3--多列布局、边框颜色、圆角、渐变效果、阴影反射