Power Strings[poj2406]题解
Power Strings
Description
- Given two strings a and b we define ab to be their concatenation. For example, if a = "abc" and b = "def" then ab = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
- Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
- For each s you should print the largest n such that s = a^n for some string a.
Sample Input 1
- abcd
aaaa
ababab
.
Sample Output 1
- 1
4
3
思路1
暴力
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max=1000001;
string a;
int x,n,m;
int p[Max];
void prime(int n)
{
int k=(int)sqrt(n*1.0);
p[1]=1;
for(int i=2; i<=k; i++)
if(n%i==0)
p[++m]=i;
int end=m;
if(k*k==n) end--;
for(int i=end; i>=1; i--)
p[++m]=n/p[i];
}
bool ok(int x)
{
string s=a.substr(0,x);
for(int i=0; i<a.size(); i+=x)
if(s!=a.substr(i,x)) return false;
return true;
}
int main()
{
while(getline(cin,a))
{
x=1,n=a.size();m=1;
prime(n);
while(x<=m)
{
if(ok(p[x]))
{cout<<a.size()/p[x]<<endl;break;}
x++;
}
}
return 0;
}
思路2
利用\(KMP\)的\(next[]\)数组
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max=1000001;
string a;
int N,M,ans,nx[Max];
void makenx(int M)
{
memset(nx,0,sizeof(nx));
int i=0,j=-1;
nx[i]=j;
while(i<M)
{
if(j==-1||a[i]==a[j]) i++,j++,nx[i]=j;
else j=nx[j];
}
}
int main()
{
bool fl;
while(getline(cin,a)&&a[0]!='.')
{
fl=true;
M=a.size();makenx(M);
if(M%(M-nx[M])==0) cout<<M/(M-nx[M])<<endl;
else cout<<1<<endl;
}
return 0;
}
Power Strings[poj2406]题解的更多相关文章
- Power Strings poj2406(神代码)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29402 Accepted: 12296 D ...
- Power Strings POJ2406 KMP 求最小循环节
相比一般KMP,构建next数组需要多循环一次,因为next[j]代表前j-1个字符的最长相同前缀后缀,比如字符串为aab aab aab共9个字符,则next[10]等于前9个字符中最长相同前缀后缀 ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- POJ2406 Power Strings —— KMP or 后缀数组 最小循环节
题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Tot ...
- poj2406 Power Strings (kmp 求最小循环字串)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47748 Accepted: 19902 ...
- poj2406 Power Strings(kmp失配函数)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...
- Power Strings(kmp妙解)
Power Strings Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
- 「UVA10298」 Power Strings(KMP
题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 abcd aaaa ababab . 输出样例#1: 复制 1 4 3 题解 Luogu的题解 这里是对目前 ...
随机推荐
- 编译安装nginx提示./configure: error: C compiler cc is not found
1 编译安装nginx提示如下 ./configure: error: C compiler cc is not found 2 解决办法 yum -y install gcc gcc-c++ aut ...
- [RHEL8]安装Docker Problem: package docker-ce-3:19.03.6-3.el7.x86_64 requires containerd.io
系统环境 # cat /etc/redhat-release Red Hat Enterprise Linux release 8.0 (Ootpa) 安装依赖 # yum install -y yu ...
- vue-cli项目传到服务器后打不开的问题
1.vue-cli项目执行dev可以打开网站,直接点击文件或发布后却打不开的问题 webpack.prod.conf.js: output: { ....... publicPath:'./' ...
- HTTP 中 GET 与 POST 的区别(详解)
我们都知道GET和POST是HTTP请求的两种基本方法,最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 很多权威网站总结出的他们的区别: GET在浏览器回退 ...
- Android 日期选择框 简洁常用
效果 核心代码 >方法 /** * @description 选择日期弹出框 * @param listener 选择日期确定后执行的接口 * @param curDate 当前显示的日期 * ...
- SQL查询结果自定义排序
一般情况之下,我们可以使用ORDER BY ...ASC或DESC来做查询排序.如: SELECT * FROM [dbo].[SalesPerformance] ORDER BY [Salesman ...
- Windows一些技巧
更改文件夹的显示名称 在要修改的文件夹下创建desktop.ini,在文件中输入: [.ShellClassInfo] LocalizedResourceName= [要显示的名字] 在CMD中输入命 ...
- Eversipn STT-MRAM的MJT细胞
业界一直在寻求取代SRAM.其中之一包括自旋转移力矩MRAM(STT-MRAM).新的存储器带来了一些大胆的主张.例如STT-MRAM具有SRAM的速度和闪存的无波动性,具有无限的耐用性. 图1.ST ...
- MySql 部分字段去重
select * from personal_question_answer where answer_id in ( select min(answer_id) from personal_ques ...
- 【Java】基于RXTX的Java串口通信
本篇内容参考转载自https://blog.csdn.net/kong_gu_you_lan/article/details/80589859 环境搭建 下载地址:http://fizzed.com/ ...