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 ...
随机推荐
- IE6 BUG margin 两倍
触发条件: 父元素包含子元素 子元素设置了浮动(float) 子元素设置了外边距(margin) 浮动方向和边距方向一致 解决方案: 给该子元素添加 display:inline;
- uiatuomator如何调试
博主较笨,在使用junit 和uiatuomator结合时不知道怎么调试,因为uiatuomator一直是push在手机上,而junit是需要代码运行的,那我该怎么办,现在发一下不知道是哪位大神写的代 ...
- Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- js ie8不支持项总结
不支持filter,trim 要用jquery 的$filter,$trim 数组不能用for in 要用for 数组没有indextOf方法 不能使用关键字,如true ,default IE8 ...
- 聊一聊 AngularJS 服务
什么是服务? 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. AngularJS 内建了30 多个服务. 为什么使用服务? 在很多服务中,比如 $loca ...
- 接口和抽象类:Interface、abstract _【转】
一.接口 接口是C#中很常见的工具,概念什么的就不说了,这里讲几个值得注意的小地方: 1.接口内部只能有函数.属性和事件的声明: interface IParent { void Show(); st ...
- APPlication,Session,Cookie,ViewState和Cache之间的区别
1.Application:用于保存所有用户共用的数据信息. 在Asp.Net中类似的配置数据最好保存在Web.config文件中.如果使用Application对象,一个需要考虑的问题是任何写操作都 ...
- Anddoi 将时间转换为指定时区的时间
import java.text.Format;import java.text.ParseException;import java.text.SimpleDateFormat;import jav ...
- .NET 进程和线程
一.进程:需要有用Process类用法一:Process.Start("calc");该方法弊端:有许多程序不知道它的运行名字到底是什么,如果写错了,就会出现程序崩溃错误用法二:/ ...
- ios fix UIRefreshControl bug
NS_CLASS_AVAILABLE_IOS(6_0) UIRefreshControl 有个毛病有时会出bug 动画下拉就不动了,这里给出修复处理: @interface UICollecti ...