wudiii的后缀
Description
现在给你一个长度为n的字符串,去掉第i个字符后的字符串为si,
请对s1,s2,s3…sn进行排序,若两个字符串相同,则编号小的更小。
例如s1=s2,则s1更小。
Input
第一行一个整数n(1<=n<=500005)。
第二行一个由n个小写字母组成的字符串。
Output
输出一行n个整数k1、k2、…kn。表示sk1<sk2<…<skn。
Sample Input 1
20 asdaaaabbdcccpppooafSample Output 1
2 3 10 14 15 16 17 18 20 19 11 12 13 8 9 4 5 6 7 1
import java.io.PrintWriter; import java.util.Scanner; public class Main { // static InputReader sc=new InputReader(System.in); static PrintWriter out=new PrintWriter(System.out); static Scanner sc=new Scanner(System.in); static int n,m; public static void main(String[] args) { // TODO Auto-generated method stub n=sc.nextInt(); String str=sc.next(); char[] ch=new char[n+1]; for(int i=0;i<n;i++) { ch[i]=str.charAt(i); } int[] node=new int[n+1]; int left=0; int right=n-1; for(int i=0;i<n;i++) { if(ch[i]<ch[i+1]) { node[right]=i+1; right--; }else if(ch[i]==ch[i+1]) { int flag=1; int cnt=0; for(int j=i;j<n;j++) { if(ch[j]>ch[i]) { int key=j; while(cnt-->0) { node[right]=key--; i++; right--; } i--; flag=0; break; }else if(ch[j]<ch[i]) { while(cnt-->0) { node[left]=i+1; i++; left++; } i--; flag=0; break; }else cnt++; } if(flag==1) { while(cnt-->0) { node[left]=i+1; i++; left++; } break; } }else { node[left]=i+1; left++; } } for(int i=0;i<n;i++) { out.print(node[i]); if(i!=(n-1)) { out.print(" "); } } out.flush(); out.close(); } } //2 3 10 14 15 16 17 18 20 19 11 12 13 8 9 4 5 6 7 1
一道wa了11次的模拟题
最新推荐文章于 2021-05-20 03:00:31 发布