博客
关于我
HDU——3374 String Problem (最大最小表示法+循环节+kmp)
阅读量:508 次
发布时间:2019-03-07

本文共 2115 字,大约阅读时间需要 7 分钟。

Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings: 

String Rank 
SKYLONG 1 
KYLONGS 2 
YLONGSK 3 
LONGSKY 4 
ONGSKYL 5 
NGSKYLO 6 
GSKYLON 7 
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once. 
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also. 

Input

  Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.

Output

Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

Sample Input

abcderaaaaaaababab

Sample Output

1 1 6 11 6 1 61 3 2 3

题意:求最大最小表示法,就是求一个字符串,输出左移补在右边的字典序最大和最小的下标和最小循环节循环的次数(输出一个下标输出一个次数)比如说样例第一个:

生成的字符串有:

1.abcder   2.bcdera  3.cderab  4.derabc  5.eabcdr  6.rabcde  所以字典序最小的是1  然后循环节次数是 1  然后字典序最大是6 然后循环节次数是 1  所以输出 1 1 6 1

题解:可以看出循环节循环次数,不论字典序最大还是最小,都是一样的~~这样利用 len/(len-nexts[len])就求出最小循环节循环次数了~~(len-nexts[len])它是最小循环节~然后最小最大的下标就用模板跑一下就好了~~上代码:

#include 
#include
using namespace std;const int MAX = 1e6+10;char str[MAX];int nexts[MAX];int len;void getnexts(){//nexts[]模板(KMP) //memset(nexts,0,sizeof(nexts));//初始,不初始化都对,还没碰上有错的 int i,j; i=j=0; nexts[0]=-1; j=-1; while(i
0) i+=k+1; else j+=k+1; if(i==j) j++; k=0; } } return min(i+1,j+1);}int posmax(int len){//最大表示法模板 int i=0,j=1,k=0; while(i
0) j+=k+1; else i+=k+1; if(i==j) j++; k=0; } } return min(i+1,j+1);}int main(){ while(~scanf("%s",str)){ len=strlen(str); getnexts(); int ci=len/(len-nexts[len]);//循环次数 cout << posmin(len) << " " << ci << " " << posmax(len) << " " << ci << endl; } return 0;}

 

转载地址:http://suqjz.baihongyu.com/

你可能感兴趣的文章
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
mysql 批量插入
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>