Match:Power Strings(POJ 2406)
2016-02-04 14:47
435 查看
字符串前缀的阶
题目大意:求前缀的阶
和POJ1961是一样的,KMP的Next数组的应用,不要用STL,不要一个一个读入字符(IO永远是最慢的)
#include <iostream> #include <algorithm> #include <functional> #include <string.h> using namespace std; static char Text[1000002]; static int _Next[1000002]; void Input(int &); void Get_Next(const int); int main(void) { int Length, k_count, if_res; while (1) { Input(Length); if (*Text == '.')//小数点结束 break; Get_Next(Length); if_res = Length % (Length - _Next[Length]); k_count = Length / (Length - _Next[Length]); if (if_res == 0 && k_count > 1) printf("%d\n", k_count); else printf("1\n"); } return EXIT_SUCCESS; } void Input(int &Length) { Length = 0; scanf("%s", Text); Length = strlen(Text); } void Get_Next(const int Length) { int i = 0, k = -1; _Next[0] = -1; while (i < Length) { if (k == -1 || Text[i] == Text[k]) { i++; k++; _Next[i] = k; } else k = _Next[k]; } }
另外以这一题有散列做法,但是没有kmp那么快,但是用到了一个非常好的算法——矩阵快速幂,以后补
相关文章推荐
- epoll案例
- 经济学--第一讲
- cookie
- 打印机 Fax 所需的驱动程序 Microsoft Shared Fax Driver 未知
- AbstractQueuedSynchronizer
- 单向链表
- oracleXE(oracle学习版)在windows的安装配置
- CDOJ 1270 Playfair(模拟)
- native2ascii 在 Mac终端的转码
- Java经典用法总结(二)
- docker升级方法
- 我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
- C中函数传递参数原理:值传递, 指针传递?
- redis的主从切换的两种方式
- Java实现Socket之WhoisClient
- 关于Activity被回收
- 适合新手动手实践的PHP搭配mysql的小项目--02实现注册页面
- Grafana-Zabbix扩展安装
- symfony2中运行周期内执行的回调函数
- Android百度地图