UVA 455 Periodic Strings (KMP && 暴力数组)
来源:互联网 发布:sql为表创建外键约束 编辑:程序博客网 时间:2024/06/06 06:31
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=396
题意:给你字符串,求最小循环节的字符个数
分析:看到关于循环节的第一反应就是KMP,其实这个题对范围要求很小,所以可以暴力,把前面的字符与后面的字符进行比较,看是否一样。最后不要忘记每一个数据后面的空行。
一:KMP
#include <cstdio>#include <cstring>using namespace std;int n;char str[100];int next[100];int len;void getNext(){ int i = 0, j = -1; next[0] = -1; while (i != len){ if (j == -1 || str[i] == str[j]){ next[++i] = ++j; } else j = next[j]; }}int main(){ scanf("%d", &n); while (n--){ scanf("%s", str); len = strlen(str); getNext(); if (len % (len - next[len]) == 0) printf("%d\n", len - next[len]); else printf("%d\n", len); if (n) printf("\n"); } return 0;}
二:数组暴力
#include <cstdio>#include <cstring>using namespace std;int n;char str[100];int main(){ scanf("%d", &n); while (n--){ scanf("%s", str); int len = strlen(str); for (int i = 1; i <= len; i++){ if (len % i == 0){ int j; for (j = i; j < len; j++){ if (str[j] != str[j % i]){ break; } } if (j == len){ printf("%d\n", i); break; } } } if (n) printf("\n"); } return 0;}
0 0
- UVA 455 Periodic Strings (KMP && 暴力数组)
- UVA 455 - Periodic Strings
- UVa 455 - Periodic Strings
- UVa 455 - Periodic Strings
- UVa 455 - Periodic Strings
- UVa 455 Periodic Strings
- UVa - 455 Periodic Strings
- UVa 455 Periodic Strings
- UVa 455 - Periodic Strings
- Uva 455-periodic strings
- Uva 455-periodic strings
- UVa-455Periodic Strings
- 【Uva 455】 Periodic Strings
- UVA - 455 Periodic Strings
- UVa 455 - Periodic Strings
- Uva - 455 - Periodic Strings
- UVA - 455 Periodic Strings
- UVa 455 - Periodic Strings
- Setting property 'source' to 'org.eclipse.jst.jee.server: ' did not find a matching property
- CSS 超出长度省略
- PXE安装RHEL6
- 算法——阶乘中含有几个0
- Hust oj 2060 截取方案数(KMP)
- UVA 455 Periodic Strings (KMP && 暴力数组)
- 2016.9.3测试解题报告(NOIP2014 day1 day2)
- Android启动流程
- JS时间格式化
- java.lang.IllegalMonitorStateException
- Java设计模式之外观模式
- thinkphp5 nginx 虚拟站点配置
- js中如何获取元素样式
- Quartz 2D编程指南之二:图形上下文(Graphics Contexts)