csuoj 1353: Guessing the Number
这个题我想到要用kmp找到循环节;
但是后面的我就不会做了;
看到题解才知道是字符串的最小表示;
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100005
using namespace std; char s[maxn*];
int next[maxn]; void kmp(int n)
{
int j=;
for(int i=;i<=n;i++)
{
while(j>&&s[i]!=s[j+])j=next[j];
if(s[i]==s[j+])++j;
next[i]=j;
}
} void MinimumRepresentation(int n)
{
int i=,j=,k=;
while()
{
if(i==j)j++;
else if(s[j]=='')j++;
else if(s[i]=='')i++;
else if(s[i+k]==s[j+k])k++;
else if(s[i+k]<s[j+k])
{
if(s[i+k]=='')j=j+k;
else j=j+k+;
k=;
}
else
{
if(s[j+k]=='')i=i+k;
else i=i+k+;
k=;
}
if(i>n||j>n||k>=n) break;
}
int t=i<=n?i:j;
for(int i=t;i<t+n;i++)
putchar(s[i]);
puts("");
} int main()
{
//freopen("test0.in","r",stdin);
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%s",s+);
n=strlen(s+);
bool flag=;
for(int i=;i<=n;i++)
if(s[i]!='')flag=;
if(flag==){printf("1%s\n",s+);continue;}
kmp(n);
int m=n-next[n];
for(int i=m+;i<=(*m);i++)
s[i]=s[i-m];
MinimumRepresentation(m);
}
return ;
}
csuoj 1353: Guessing the Number的更多相关文章
- [coj 1353 Guessing the Number]kmp,字符串最小表示法
题意:给一个字符串,求它的最小子串,使得原串是通过它重复得到的字符串的一个子串. 思路:先求最小长度,最小循环长度可以利用kmp的next数组快速得到,求出长度后然后利用字符串最小表示法求循环节的最小 ...
- csuoj 1392: Number Trick
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1392 1392: Number Trick Time Limit: 1 Sec Memory L ...
- CSUOJ 1299 - Number Transformation II 打表预处理水DP
http://122.207.68.93/OnlineJudge/problem.php?id=1299 第二个样例解释.. 3 6 3->4->6..两步.. 由此可以BFS也可以DP. ...
- Leetcode: Guess Number Higher or Lower II
e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...
- Codeforces Gym 100015G Guessing Game 差分约束
Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...
- Palindromic Number (还是大数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- [USACO 08JAN]Haybale Guessing
Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...
- [USACO08JAN]haybale猜测Haybale Guessing
题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...
- PAT A1024 Palindromic Number (25 分)——回文,大整数
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
随机推荐
- 去除ActionBar的方法
今天做一个播放器项目,由于要去除ActionBar,纠结好久,原来这么简单 记录一下 只需要修改AndroidManifest.xml文件中的主题即可 <application android: ...
- length prototype 函数function的属性,以及构造函数
前言:学到一些JavaScript高级的知识,在这里记下,方便以后的查找 1.length代表函数定义的形参的个数,挺简单的 例如:function Pen(price,cname) { . ...
- C#学习笔记10:Try-catch的用法和引用类型、值类型整理
Try-Catch: 将可能发生异常的代码放到try中,在catch中进行捕获. 如果try中有一行代码发生了异常,那么这行代码后面的代码不会再被执行了. Try写完了以后,紧接着就要写Catch ...
- ACM——01排序
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1024 01排序 时间限制(普通/Jav ...
- ArryList vs LinkedList
references: http://www.javaperformancetuning.com/articles/randomaccess.shtml http://stackoverflow.co ...
- 关于四字节字符入库时错误的解决方案(Incorrect string value: '\xF0\x9F\x99\x8F' for column 'Reply_Content' at row 1)
1. 将表字段字符集设置成utf8mb4 2. 执行插入前执行:SET NAMES utf8mb4; 如: SET NAMES utf8mb4; INSERT test(Content) VALUES ...
- mac 查看系统时区
sudo systemsetup -gettimezone https://developer.apple.com/library/mac/documentation/Darwin/Reference ...
- 使用info.plist(或工程名-info.plist)向程序中添加软件Build ID或者版本号信息
在实际应用程序开发过程中,经常需要向程序中添加软件版本号或者类似的信息,以保证之后发现问题时知道bug所在的版本,我们可以通过在工程名-info.plist文件中设置相关的key/value对(键/值 ...
- python 自动化之路 day 01 人生若只如初见
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 Python 注释 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式i ...
- linux修改时间 时区
查看时区:date -R 修改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 二.时间 1.查看时间和日期 date 2.设置时间和日期 ...