AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 3451. 字符串排序II 0.01 AC币

作者: 作者的头像   LaLa_JUROU ,  2023-05-24 12:31:57 ,  所有人可见 ,  阅读 9


0


//二维数组的排序
//二维dp,空间换时间
import java.io.*;

public class Main{
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int dp[][],sort[]; //dp的索引0存储数组长度
    static char line[];
    public static void main(String[] args) throws IOException{
        String sr = in.readLine();
        while(sr != null) {
        line = sr.toCharArray();
        dp = new int[100][10001];
        sort = new int[line.length + 1];
        for(int i = 0 ; i < sort.length; i ++) {//预处理
            sort[i] = -1;
        }
        for(int i = 0; i < line.length; i ++) {//动态分组
            if(line[i] >= 65 && line[i] <= 122) {
                int index = (line[i] - 65) % 32; //不区分大小写
                dp[index][dp[index][0] + 1] = line[i];
                dp[index][0] ++;
            } else {
                sort[i] = line[i];
            }
        }
        //将字符重新排列到sort中
        int index = 0;
        for(int i = 0; i < dp.length; i ++) {
            for(int o = 1; o < dp[i].length; o ++) {
                while(sort[index] != -1) {//指针移动到需要插入的位置
                    index ++;
                }
                if(o > dp[i][0]) {
                    break;
                }
                sort[index] = (char)dp[i][o];   
            }
        }
        for(int i = 0; i < sort.length - 1; i ++) {
            out.print((char)sort[i]);
        }
        out.println();
        out.flush();
    }
    }
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息