题意:给一个字符串,求它的最小子串,使得原串是通过它重复得到的字符串的一个子串。

思路:先求最小长度,最小循环长度可以利用kmp的next数组快速得到,求出长度后然后利用字符串最小表示法求循环节的最小表示即可。

#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fillarray(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; #ifndef ONLINE_JUDGE
namespace Debug {
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ const int maxn = 2e5 + ; struct KMP {
int next[maxn];
void GetNext(char s[]) {
fillchar(next, );
next[] = next[] = ;
for(int i = ; s[i]; i++) {
int j = next[i];
while(j && s[i] != s[j]) j = next[j];
next[i + ] = s[j] == s[i]? j + : ;
}
}
};
KMP kmp; char s[maxn]; void work(char s[], int n) {
int i = , j = ; /** 用i存最小表示的最小可能位置, 同时令j>i,因为0~i-1都不是最小表示*/
while (i < n && j < n) {
while (s[i] == '0') i ++;
umax(j, i + );
while (s[j] == '0') j ++;
//Debug::print(i, j, n);
if (j >= n) break;
int k = ;
while (s[i + k] == s[j + k] && k <= n) k ++;
if (s[i + k] > s[j + k]) if (s[j + k] != '0') i += k + ; else i += k;
else if (s[i + k] != '0') j += k + ; else j += k;
//Debug::print(i, j, n);
}
int x = i < n? i : j; /** 最后一次可能跳跃导致i大于等于n,那么此时的j一定是最小表示 */
for (int i = x; i < x + n; i ++) {
putchar(s[i]);
}
putchar('\n');
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T;
cin >> T;
while (T --) {
scanf("%s", s);
int n = strlen(s);
bool ok = false;
for (int i = ; s[i]; i ++) {
if (s[i] != '0') {
ok = true;
break;
}
}
if (!ok) {
s[n ++] = '1';
s[n] = ;
}
kmp.GetNext(s);
int len = n - kmp.next[n];
for (int i = len; i < * len; i ++) s[i] = s[i - len];
s[ * len] = ;
work(s, len);
}
return ;
}

[coj 1353 Guessing the Number]kmp,字符串最小表示法的更多相关文章

  1. O - String Problem KMP 字符串最小表示法

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  2. bzoj5130 字符串的周期(kmp,最小表示法)

    bzoj5130 字符串的周期(kmp,最小表示法) bzoj 题解时间 m很大,n很小. 周期很容易求,就是kmp之后n-fail[n]. 之后对于枚举所有的字符串用最小表示法,暴力搜索. 能过就完 ...

  3. 牛客练习赛36 A Rabbit的字符串(字符串最小表示法)

    链接:https://ac.nowcoder.com/acm/contest/328/A来源:牛客网 题目描述 Rabbit得到了一个字符串,她的好朋友xxx可以给这个字符串施加一次魔法. 魔法可以选 ...

  4. bzoj2176 Strange string(字符串最小表示法)

    Time Limit: 10 Sec  Memory Limit: 259 MB 给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的 ...

  5. hdu3374 String Problem KMP+最大最小表示法

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  6. BZOJ1398: Vijos1382寻找主人 Necklace 字符串最小表示法

    Description 给定两个项链的表示,判断他们是否可能是一条项链. Input 输入文件只有两行,每行一个由0至9组成的字符串,描述一个项链的表示(保证项链的长度是相等的). Output 如果 ...

  7. Manacher模板,kmp,扩展kmp,最小表示法模板

    *N]; //储存临时串 *N];//中间记录 int Manacher(char tmp[]) { int len=strlen(tmp); ; ;i<len;i++) { s[cnt++]= ...

  8. hdu 3374 String Problem (kmp+最大最小表示法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小 ...

  9. POJ 1509 Glass Beads【字符串最小表示法】

    题目链接: http://poj.org/problem?id=1509 题意: 求循环字符串的最小表示. 分析: 浅析"最小表示法"思想在字符串循环同构问题中的应用 判断两字符串 ...

随机推荐

  1. JavaScript的运行机制!!!很重要很重要!!!!!!请看大神操作!

    https://juejin.im/post/59e85eebf265da430d571f89

  2. 【山外问道】Linux UUID的查询方法

    本文打印版下载地址 [山外问道]Linux_UUID的查询方法-打印版.pdf 一.查询存储设备的UUID 1.使用blkid命令查看 (1)查询所有存储设备的UUID:blkid. (2)查询指定设 ...

  3. 最简单的懒人springcloud之Eureka(服务注册与发现)

    本文开发软件是STS,是eclipse为springboot项目而生的一个软件,用这个软件开发spring的项目版本都会自己对应的,话不多说直接上代码 springboot版本2.1.8.RELEAS ...

  4. mac 使用记录

    iterm 配合 lrzsz 实现 上传下载

  5. python数据分析工具——Pandas、StatsModels、Scikit-Learn

    Pandas Pandas是 Python下最强大的数据分析和探索工具.它包含高级的数据结构和精巧的工具,使得在 Python中处理数据非常快速和简单. Pandas构建在 Numpy之上,它使得以 ...

  6. 关于join on 和单表查询的实时效果

    当数据量大(10W单位级)的时候,join的优势,会被单表查询超过. 以下是两张表单查和两张表联查的时间对比,同时,这样的记录有局限性的. 一.数据量少时: 单表查: 表一:显示行 0 - 2 ( 3 ...

  7. Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \;

    find命令的参数: pathname: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录.-print: find命令将匹配的文件输出到标准输出.-exec: find命令 ...

  8. 0day学习笔记(2)--函数调用

    函数调用过程 调用函数操作 函数参数入栈(在当前函数栈帧),从左至右或从右至左视情况而定 一般为从右至左 mov 地址,参数 的一个操作并不直接pop而是定位到地址将参数传递进去 call offse ...

  9. linux--配置开发环境 --Nginx篇

    安装: 安装好了话,我们的nginx的目录在:  /etc/nginx 启动: sudo service nginx start 然后访问我们的页面就可以看到了我们的界面 然后我们配置我们的域名: 我 ...

  10. vue 开发规范

    本文档为前端 vue 开发规范 规范目的 命名规范 结构化规范 注释规范 编码规范 CSS 规范 规范目的 为提高团队协作效率 便于后台人员添加功能及前端后期优化维护 输出高质量的文档 命名规范 为了 ...