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 最小字符串表示的更多相关文章

  1. SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串

    Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...

  2. 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 ...

  3. 寻找最小字符串,IP地址——解题报告

    寻找最小字符串 题目 思路 在寻找最小字符串的时候需要用到的函数有strcmp和strcpy,首先先输入输入字符串,先假设第一个字符串为最小的字符串min,再然比较接下来的字符串,当接下来的字符串比m ...

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

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

  5. POJ1509 Glass Beads(最小表示法 后缀自动机)

    Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4901   Accepted: 2765 Description Once ...

  6. UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)

    题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...

  7. [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  ...

  8. [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 ...

  9. [poj1509]Glass Beads(最小表示法)

    题目大意:求循环同构的字符串的最小字典序. 解题关键:最小表示法模板题. #include<cstdio> #include<cstring> #include<algo ...

随机推荐

  1. LINUX系统---初级相关操作和知识

    LINUX系统的初级,从安装LINUX开始,到处理简单的运维问题.搭建各种服务.解决网路问题.缓解服务器压力,写简单的shell脚本. 我们从基本的入门开始搞事情: 安装LINUX系统 对磁盘的使用 ...

  2. 85-Momentum 动量指标.(2015.7.3)

    Momentum 动量指标 动量数值就是当天价格同前几个时段的价格的比率 MOMENTUM = CLOSE(i)/CLOSE(i-N)*100 注解: CLOSE(i) - 当前柱形的收市价格: CL ...

  3. 圆角计算 Shader

    圆角的计算 在Shader中,我们使用UV坐标来计算需要显示的部分和不需要显示的部分,使用透明来处理显示与不显示.UV坐标如下图1,我们将坐标平移到图2位置,面片的UV坐标原点在面片中心,UV坐标范围 ...

  4. Mac 共享无线网络

    dock栏启动『设置』进入下一步.   进入设置后,点击『共享』进入下一步.   进入共享后 1.选择左边框中『互联网共享』,就会看到截图右边的样子 2.『共享以下来源链接』一项选择『以太网』(默认应 ...

  5. Codeforces Round #355 (Div. 2)-B. Vanya and Food Processor,纯考思路~~

    B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. Amoeba新版本MYSQL读写分离配置

    标签:mysql 数据库 读写分离 休闲 amoeba 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://skyson.blog.5 ...

  7. Chrome & CORS & Fetch API & Chrome 多开,应用分身

    Chrome & CORS & Fetch API Chrome 浏览器的跨域设置 https://www.cnblogs.com/cshi/p/5660039.html https: ...

  8. [luoguP1033] 自由落体(模拟?)

    传送门 这不能算是数论题... 卡精度这事noip也做的出来.. 代码 #include <cmath> #include <cstdio> int n, ans; doubl ...

  9. android studio配置so文件路径

    将一个项目从eclipse上移植到android studio时,发现总是加载不成功库文件,so库文件放在了main/src/libs下的目录. 参考网上资料,studio默认的库文件路径是main/ ...

  10. 关于FFM (Field-aware Factorization Machines) 的几篇博客

    深入FFM原理与实践 http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html#mjx-eqn-e ...