2

Java でジェネレーター クラスを作成して、次の最小の一意の ascii 文字列を生成したいと考えています。結果の文字列は aZ で始まる必要があり、後続の各文字は aZ または 0-9 にすることができます。

文字列は、JavaScript ファイル内の変数を縮小するために使用されています。

これを行うツールや、実装方法に関する大まかなコードに関する提案はありますか?

4

2 に答える 2

1

別の文字を使用する必要がある場合は、使用できます

public static void main(String... ignored) {
    String prev = "";
    for (int i = 0; i < 40000000; i++) {
        String s = asId(i);
        if (s.length() > prev.length())
            System.out.print(prev + "\n" + s + " to ");
        prev = s;
    }
}

static char[] CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

public static String asId(long number) {
    StringBuilder sb = new StringBuilder();
    long div = number < 52 ? 1 : 52;
    while (div <= number / 62) div *= 62;
    while (div > 0) {
        sb.append(CHARS[((int) (number / div % 62))]);
        div /= 62;
    }
    return sb.toString();
}

プリント

0 to Z
10 to ZZ
100 to ZZZ
1000 to 

Javaでは以下を使用できます。

public static String asId(long number) {
    return (char) ('a' + number % 26) 
        + (number >= 26 ? Long.toString(number / 26, 36) : "");
}

負の数が気になる場合は、使用できます。

public static String asId(long number) {
    long lowBit = number & 1;
    long highBits = number >>> 1;
    return (char) ('a' + highBits % 13 + lowBit) 
        + (number >= 26 ? Long.toString(highBits / 13, 36) : "");
}
于 2013-01-07T19:51:31.447 に答える
0

ここにいくつかのアイデアがあります(完全にはテストされていません!!!)

import java.lang.*;
import java.util.*;

public class Test
{
    static class VariablesIterator implements Iterator<String>
    {
        private List<Character> characters = new ArrayList<Character>();

        private List<Integer> indices = new ArrayList<Integer>();

        public VariablesIterator(String start)
        {
            char[] cs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

            for (char c : cs)
            {
                characters.add(c);
            }

            for (int i = 0; i < start.length(); ++i)
            {
                indices.add(characters.indexOf(start.charAt(i)));
            }
        }

        public boolean hasNext()
        {
            return true;
        }

        public String next()
        {
            String current = "";

            for (int i = 0; i < indices.size(); ++i)
            {
                current += characters.get(indices.get(i));
            }

            Integer last = indices.get(indices.size() - 1);

            if (indices.size() != 1 && last != 2*26 + 10 - 1 || indices.size() == 1 && last != 2*26 - 1)
            {
                indices.set(indices.size() - 1, last + 1);
            }
            else
            {
                indices.add(0);
            }

            return current;
        }

        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }

    static class Variables implements Iterable<String>
    {
        public Iterator<String> iterator()
        {
            return new VariablesIterator("a");
        }
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        for (String variable : new Variables())
        {
            System.out.println(variable);
            in.nextLine();
        }
    }
}
于 2013-01-07T20:21:16.070 に答える