POJ2406 Power String
2015-11-26 09:22
176 查看
这道题的大意是:
给出若干字符串,求每个字符串的最小构成子串(从字符串的头开始),即由该子串不断重复能构成所给出的字符串。
主要是kmp的应用。
给出若干字符串,求每个字符串的最小构成子串(从字符串的头开始),即由该子串不断重复能构成所给出的字符串。
主要是kmp的应用。
首先,使用暴力的方法能过。时间复杂度高,空间复杂度低。 枚举子串的长度,该长度应能被整个字符串的长度整除。将子串与字符串遍历比较,若有不同则枚举下一个子串。对子串可以用加一模除整个子串长度,来扩展成语字符串相同的长度。
#include <algorithm> #include <string.h> #include <math.h> #include <stdio.h> const int max = 10000001; char Str[max]; int main() { while (scanf("%s", Str) && Str[0] != '.') { int chnum = 1; int len = strlen(Str); for (chnum = 1; chnum <= len; chnum++) { int cishu = len%chnum; if (cishu) continue; if (chnum == len) { printf("1\n"); break; } int i = 0, j = 0; for (i = 0; i < len; i++) { if (Str[i] != Str[j]) break; else j = (j+1)%chnum; } if (i >= len) { printf("%d\n", len/chnum); break; } } } return 0; }
可以用kmp算法中,构造next数组的思想。 next数组存放的是当前字符要与之比较的字符,在这两个字符之前的字符都相同。 当每个字符均不同时要特判。
#include <algorithm> #include <string.h> #include <math.h> #include <stdio.h> const int max = 10000001; char Str[max]; int next[max]; void get_next(int len) { int i = 0, j = -1; next[0] = -1; while(i < len) { if (j == -1 || Str[i] == Str[j]) { ++i; ++j; next[i] = j; } else j = next[j]; } return; } int main() { while (scanf("%s", Str) && Str[0] != '.') { int len = strlen(Str); get_next(len); int num = len-next[len]; if (len%(num) == 0) printf("%d\n", len/(num)); else printf("1\n"); } return 0; }
相关文章推荐
- Camera.culling mask
- 浅谈C++中的Const
- iOS-Core-Animation-Advanced-Techniques
- 【读书笔记】iOS-iCloud编程
- Bootstrap中的面板
- 【读书笔记】iOS-iCloud编程
- Apache Commons 工具类介绍及简单使用
- BLE睡眠分析(一)
- LeetCode OJ 系列之122 Best Time to Buy and Sell Stock II --Python
- OC_protocol协议介绍(1)
- 普通排序--冒泡排序
- 站长留言
- Beta版本冲刺计划及安排
- Bootstrap历练实例:分页状态
- Bootstrap历练实例:分页的大小
- css布局你该了解的
- Windows Server 2008 部署webservice(C# .Net)
- x86、ARM和MIPS三种主流芯片架构
- iOS开发笔记-UITableView的单选
- Oracle数据文件/控制文件/日志文件