SPOJ BEADS 最小字符串表示
SPOJ BEADS 给一个字符串(环) 问从哪个字符开始,字典序最小。
可以脑补到很多线性的解法,不过以下这个是最简单的,代码非常简单,就不解释了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<cmath>
using namespace std; int smallestRepresation(string& s)
{
int i,j,k,l;
int N=s.length();
s+=s;
for(i=0,j=1;j<N;)
{
for(k=0;k<N&&s[i+k]==s[j+k];k++);
if(k>=N)break;
if(s[i+k]<s[j+k]) j+=k+1;
else
{
l=i+k;
i=j;
j=max(l,j)+1;
}
}
return i;
} int main()
{
ios::sync_with_stdio(false);
freopen("t.txt","r",stdin);
int Q;
cin>>Q;
while(Q--)
{
string s;
cin>>s;
cout<<smallestRepresation(s)+1;
if(Q>0)cout<<endl;
}
return 0;
}
SPOJ BEADS 最小字符串表示的更多相关文章
- SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串
Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- 寻找最小字符串,IP地址——解题报告
寻找最小字符串 题目 思路 在寻找最小字符串的时候需要用到的函数有strcmp和strcpy,首先先输入输入字符串,先假设第一个字符串为最小的字符串min,再然比较接下来的字符串,当接下来的字符串比m ...
- POJ 1509 Glass Beads【字符串最小表示法】
题目链接: http://poj.org/problem?id=1509 题意: 求循环字符串的最小表示. 分析: 浅析"最小表示法"思想在字符串循环同构问题中的应用 判断两字符串 ...
- POJ1509 Glass Beads(最小表示法 后缀自动机)
Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4901 Accepted: 2765 Description Once ...
- UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)
题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...
- [Swift]LeetCode988. 从叶结点开始的最小字符串 | Smallest String Starting From Leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [poj1509]Glass Beads(最小表示法)
题目大意:求循环同构的字符串的最小字典序. 解题关键:最小表示法模板题. #include<cstdio> #include<cstring> #include<algo ...
随机推荐
- Why Countries Succeed and Fail Economically
Countries Succeed and Fail Economically(第一部分)" title="Why Countries Succeed and Fail Econo ...
- (贪心) Vacations 求休息的最少天数
Description Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasy ...
- I2C详细介绍
I2C时序 1.开始和停止: 说明: 开始:在SCL的高电平的时候SDA线的从高电平到低电平的跳变定义为开始 停止:在SCL的高电平的时候SDA线的从低电平到高电平的跳变定义为停止 2.有效数据的位置 ...
- python +selenium的 里面还自带case 然后也有生成报告的模板
https://github.com/huahuijay/python-selenium2这个就是 python +selenium的 里面还自带case 然后也有生成报告的模板
- 慕课笔记利用css进行布局【混合布局练习】
通过学习div的布局,以一个简单的内容管理网站的布局为例子,用div+css进行简单的网页布局,加深学印象: <html> <head> <title>CSS+di ...
- 动态链接 - dll和so文件区别与构成
动态链接,在可执行文件装载时或运行时,由操作系统的装载程序加载库.大多数操作系统将解析外部引用(比如库)作为加载过程的一部分.在这些系统上,可执行文件包含一个叫做import directory的 ...
- nyoj_176_整数划分(二)_201404261715
整数划分(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 把一个正整数m分成n个正整数的和,有多少种分法? 例:把5分成3个正正数的和,有两种分法: 1 1 3 ...
- CentOS7下安装单机版RabbitMQ及权限赋予
RabbitMQ官网rpm软件包地址:https://www.rabbitmq.com/releases/ 一.安装环境: CentOS7.erlang-19.0.4-1.el7.centos.x86 ...
- Rooks-LightOj1005(规律)
A rook is a piece used in the game of chess which is played on a board of square grids. A rook can o ...
- codevs 3498 小木棍
3498 小木棍 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 乔治有一些同样长的小木棍,他把这些木棍随意 ...