One day Silence is interested in revolving the digits of a positive integer. In the revolving operation, he can put several last digits to the front of the integer. Of course, he can put all the digits to the front, so he will get the integer itself. For example, he can change 123 into 312, 231 and 123. Now he wanted to know how many different integers he can get that is less than the original integer, how many different integers he can get that is equal to the original integer and how many different integers he can get that is greater than the original integer. We will ensure that the original integer is positive and it has no leading zeros, but if we get an integer with some leading zeros by revolving the digits, we will regard the new integer as it has no leading zeros. For example, if the original integer is 104, we can get 410, 41 and 104.

InputThe first line of the input contains an integer T (1<=T<=50) which means the number of test cases. 
For each test cases, there is only one line that is the original integer N. we will ensure that N is an positive integer without leading zeros and N is less than 10^100000.OutputFor each test case, please output a line which is "Case X: L E G", X means the number of the test case. And L means the number of integers is less than N that we can get by revolving digits. E means the number of integers is equal to N. G means the number of integers is greater than N.Sample Input

1
341

Sample Output

Case 1: 1 1 1

题意:给定一个数字,问旋转后有多少个不同的数字小于它本身,等于它本身,大于它本身。

思路:比较两个数的大小时,我们可以跳过前面相等的数字,直接比较第一个不相等的数字,那么就可以用exKMP,得到expand的位置,然后比较第一个不相等的位置。因为是求不同的数字的贡献,我们还要注意循环节。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int Next[maxn],ans1,ans2,ans3; char c[maxn];
void exKMP(){
int i,len=strlen(c+);
Next[]=len;
for(int i=;i<=len;i++) c[i+len]=c[i];
for(i=;i+<len+len&&c[i+]==c[i+];i++);
Next[]=i; int a=;
for(int k=;k<=len+len;k++){
int p=a+Next[a]-, L=Next[k-a+];
if(L>=p-k+){
int j=(p-k+)>?(p-k+):;
while(k+j<=len+len&&c[k+j]==c[j+]) j++;
Next[k]=j, a=k;
}
else Next[k]=L;
}
ans1=ans2=ans3=;
for(int i=;i<=len;i++){
if(Next[i]>=len){ if(ans2) break; ans2++; }
else {
if(c[i+Next[i]]<c[Next[i]+]) ans1++;
else ans3++;
}
}
}
int main(){
int T,Cas=;
scanf("%d",&T);
while(T--){
scanf("%s",c+);
exKMP();
printf("Case %d: %d %d %d\n",++Cas,ans1,ans2,ans3);
}
return ;
}

最新文章

  1. python sokct 包详解
  2. 使用centos引导内核错误:kernel: pnp 00:0b: can&#39;t evaluate _CRS: 8
  3. 2-ls 显示目录内容
  4. golang channel buffer
  5. Java-Stack
  6. 使用gradle创建java程序
  7. CodeForces 617C【序枚举】
  8. C# 之 FileSystemWatcher事件多次触发的解决方法
  9. centos7搭建NIS与NFS综合应用
  10. Android中的测试类配置AndroidManifest.xml
  11. mysql配置文件转载
  12. Linux中fork()函数详解(转载)
  13. 最想做的三个Delphi项目:Paint,IM,SQL,另外还有Smart,TMS,FMX,UML,FreePascal,Python4Delphi,Cheat Engine
  14. C#HTTP代理的实现之注册表实现
  15. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
  16. 使用MyBatis时接收值和返回值选择Map类型或者实体类型
  17. [bzoj1369] [Baltic2003]Gem
  18. bootstrap-table分页数据前台不显示
  19. chrome浏览器快捷键大全
  20. Kafka配置说明

热门文章

  1. Python单元测试框架——unittest
  2. Openstack之Nova创建虚机流程分析
  3. shell脚本小示例
  4. 深入理解PHP之:Nginx 与 FPM 的工作机制
  5. [SCOI2005]扫雷Mine
  6. 求逆元 HDU 2516
  7. application pool can not write to event log
  8. Spring与CXF整合
  9. C++ dll的隐式与显式调用
  10. Android -- SQLite 数据库创建,增删改查,事务处理