4

嘿,所以为了练习,我发现了这个我现在已经工作了几天的编码挑战。我有第一部分,但我似乎无法弄清楚如何从我所在的位置继续。这是挑战:

    Consider a "word" as any sequence of capital letters A-Z (not limited to 
just "dictionary words"). For any word with at least two different letters, 
there are other words composed of the same letters but in a different order (for 
instance, STATIONARILY/ANTIROYALIST, which happen to both be dictionary words; 
for our purposes "AAIILNORSTTY" is also a "word" composed of the same letters as
these two).

    We can then assign a number to every word, based on where it falls in an 
alphabetically sorted list of all words made up of the same set of letters. One
 way to do this would be to generate the entire list of words and find the 
desired one, but this would be slow if the word is long.
    Write a program which takes a word as a command line argument and prints to 
standard output its number. Do not use the method above of generating the entire
 list. Your program should be able to accept any word 20 letters or less in 
length (possibly with some letters repeated), and should use no more than 1 GB
 of memory and take no more than 500 milliseconds to run. Any answer we check 
will fit in a 64-bit integer.

Sample words, with their rank:
ABAB = 2 
AAAB = 1 
BAAA = 4 
QUESTION = 24572 
BOOKKEEPER = 10743 
NONINTUITIVENESS = 8222334634

    Your program will be judged on how fast it runs and how clearly the code is 
written. We will be running your program as well as reading the source code, so 
anything you can do to make this process easier would be appreciated.

到目前为止,如果所有字母都不同,我的代码可以返回正确答案。这是我的代码:

import java.util.Arrays;
import java.util.Scanner;
public class AthenaDility {

    public static void main (String[] args) {
        //Finds word that is entered
        Scanner scan = new Scanner (System.in);
        String word = scan.next();
        scan.close();

        //added value
        int value = 1;

        //alphabetical representation
        char[] charm = word.toCharArray();
        char[] alphaCharm = word.toCharArray(); 
        Arrays.sort(alphaCharm);

        //Comparer
        for (int m = 0; m < word.length(); m++) {
            for (int c = 0; c < word.length()-1; c++) {
                System.out.println(charm[m] + " " + alphaCharm[c]);

                //Skips if alphaCharm is a space
                if (alphaCharm[c] == '-') {
                }

                //If the same letter it breaks look and begins next
                else if (charm[m] == alphaCharm[c]) {
                    System.out.println("Deleting: " + alphaCharm[c]);
                    alphaCharm[c] = '-'; //Delete letter for it is used and cannot be used to compare at later points
                    break;
                }

                //if the letter in alphaCharm comes before charm
                else if (charm[m] > alphaCharm[c]){
                    System.out.println("Found!");

                    //factorial calculation
                    int factorial = 1;

                    //takes the length of the word minus the current location and one after for factorial
                    for (int f = word.length() - m - 1; f > 0; f--) {
                        System.out.print(f + " ");
                        factorial *= f;
                    }
                    //end loop
                    //Adding to others
                    System.out.println("\n" + "Factorial: " + factorial);
                    value += factorial;
                }
                else {

                }
            }
        }

        //Result
        System.out.println("end: " + value);
    }

}   

为了尽可能简单地解释它,它创建了两个字符串:一个是按字母顺序排列的字母,一个是原始单词。然后,该程序一次比较每个字母,并且在原始单词中按字母顺序出现在该字母之前的任何字母都会对第一个单词之前存在的组合数量进行阶乘计算。

我需要帮助考虑的部分是输入的字符串是否有多个相同的字母。我确实花了几天时间试图弄清楚这一点。先感谢您!

ps 代码中有很多 System.out.println 用于测试

4

0 回答 0