/**
题目:F. String Compression
链接:http://codeforces.com/problemset/problem/825/F
题意:压缩字符串后求最小长度。
思路:
dp[i]表示前i个字符需要的最小次数。
dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x。w = digit((i-j)/x)+x;
否则w = i-j+1; 求一个区间最小循环节:
证明:http://www.cnblogs.com/chenxiwenruo/p/3546457.html
KMP最小循环节、循环周期: 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为len-next[len],子串为S[0…len-next[len]-1]。 如果next[len]==0,T = len/L = 1; 说明自身就是循环节。周期为1. (1)如果len%(len - next[len])==0,则表明字符串S可以完全由循环节循环组成,循环周期T=len/L。(这里的len就是从0开始的len位置,kmp也计算了next[len]。) (2)如果不能,说明还需要再添加几个字母才能补全。需要补的个数是循环个数L-len%L=L-(len-L)%L=L-next[len]%L,L=len-next[len]。
*/ #include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
#define ms(x,y) memset(x,y,sizeof x)
const int N = 8e3 + ;
const int INF = 0x3f3f3f3f;
char s[N];
int f[N];
int dp[N];
int w[N][N];///w[i][j]表示[i,j]字符串的价值。
int digit(int x)
{
int cnt = ;
while(x){
cnt++;
x /= ;
}
return cnt;
}
void getFail(char *p,int* f)
{
int m = strlen(p);
f[] = f[] = ;
for(int i = ; i < m; i++){
int j = f[i];
while(j&&p[i]!=p[j]) j = f[j];
f[i+] = (p[i]==p[j])?j+:;
}
}
void solve(char *s)
{
int n = strlen(s);
for(int i = ; i < n; i++) w[i][i] = ;
for(int i = ; i < n; i++){
getFail(s+i,f);
for(int j = i+; j <= n; j++){
int len = j-i;
int d = len-f[j-i];
if(f[j-i]==||len%d!=){
w[i][j-] = len+;
}else
{
w[i][j-] = digit(len/d)+d;
}
}
}
}
int main()
{
while(scanf("%s",s)==)
{
solve(s);
int n = strlen(s);
dp[] = w[][];
for(int i = ; i < n; i++){
dp[i] = w[][i];///整段[0,i]。
for(int j = ; j < i; j++){
dp[i] = min(dp[i],dp[j]+w[j+][i]);
}
}
printf("%d\n",dp[n-]);
} return ;
}

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节的更多相关文章

  1. poj1961 Period kmp解决找字符串的最小循环节

    /** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...

  2. kmp的next数组的运用(求字符串的最小循环节)

    hdu3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  3. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  4. Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  5. Cyclic Nacklace hdu3746 kmp 最小循环节

    题意:给出一段字符串  求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是:  如果nex[len]为0  那么答案显然是补len 否则  答案为循环 ...

  6. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  7. 【KMP+最小循环节】F. Cyclic Nacklace

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/F [题意] 给定一个字符串,问在字符串后最少添加多少个字母,得到的新字符串能是前 ...

  8. Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)

    http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...

  9. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. centos7安装后的防火墙问题

    centos7 默认使用firewall作为防火墙 停止并关闭开机自启动: systemctl stop firewalld.service #停止firewall systemctl disable ...

  2. ElasticSearch reindex报错:the final mapping would have more than 1 type

    ElasticSearch reindex报错:the final mapping would have more than 1 type 学习了:https://blog.csdn.net/qq_2 ...

  3. [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin

    As you refactor and modify applications, it's difficult to manage and keep track of files as they be ...

  4. [Functional Programming Monad] Combine Stateful Computations Using Composition

    We explore a means to represent the combination of our stateful computations using familiar composit ...

  5. web页面内容优化管理与性能技巧

    来源:GBin1.com 回 想一下,以前我们不得不花费大量时间去优化页面内容(图片.CSS等等),如今用户有更快速的互联网链接,我们似乎能够使用更大的图像或更大的闪 存文件,里面包含的有视频或者图片 ...

  6. Android秒级编译工具Freeline

    Freeline 是 Android 平台上的秒级编译方案,Instant Run 的替代品,由蚂蚁聚宝Android 团队开发,它可以充分利用缓存文件,在几秒钟内迅速地对代码的改动进行编译并部署到设 ...

  7. org.eclipse.e4.core.di.InjectionException:org.eclipse.swt.SWTException: Widget is disposed

    org.eclipse.e4.core.di.InjectionException:org.eclipse.swt.SWTException: Widget is disposed 开发环境为ecli ...

  8. 算法笔记_091:蓝桥杯练习 递推求值(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) + 3F(n- ...

  9. java new map

    import com.google.common.collect.Maps; public static Map<String, Object> configMap2 = new Conc ...

  10. JavaWeb 发送post请求的2种方式(form、json)

      JavaWeb 发送post请求的2种方式(form.json) CreationTime--2018年6月20日10点15分 Author:Marydon 前提:通过HttpClient来实现 ...