NC17244. E、Sort String
描述
输入描述
Input contains only one line consisting of a string S.
1≤ |S|≤ 106
S only contains lowercase English letters(i.e. ).
输出描述
First, output one line containing an integer K indicating the number of lists.
For each following K lines, output each list in lexicographical order.
For each list, output its length followed by the indexes in it separated by a single space.
示例1
输入:
abab
输出:
2 2 0 2 2 1 3
示例2
输入:
deadbeef
输出:
8 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7
C++14(g++5.4) 解法, 执行用时: 226ms, 内存消耗: 9996K, 提交时间: 2018-07-27 10:06:56
#include<bits/stdc++.h> char s[1000001]; int main(){ scanf("%s",s); int len=strlen(s),x,i,j; for(x=1;x<=len/2;x++) if(len%x==0){ bool ok=1; for(i=0;i<len&&ok;i++) if(s[i]!=s[i%x])ok=0; if(ok)break; } if(x>len/2)x=len; printf("%d\n",x); for(i=0;i<x;i++){ printf("%d",len/x); for(j=0;j<len/x;j++) printf(" %d",i+j*x); puts(""); } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 260ms, 内存消耗: 15924K, 提交时间: 2020-02-28 14:09:17
#include<bits/stdc++.h> const int maxn=1e6+10,mo=1e9+7; char s[maxn]; int f[maxn],n,g; int main() { scanf("%s",s+1); n=strlen(s+1); f[0]=-1; for(int i=1,j=-1;i<=n;f[i++]=++j) while(j>=0&&s[j+1]!=s[i]) j=f[j]; g=(n%(n-f[n])==0)?n-f[n]:n; printf("%d\n",g); for(int i=0;i<g;i++) { printf("%d ",n/g); for(int j=i;j<n;j+=g) printf("%d ",j); puts(""); } }