简体   繁体   English

程序找到重复n个字符的字符串中的'a'数?

[英]Program to find the number of 'a' in a string that is repeated n characters?

I am writing a program to find the number of 'a' in a given string that is repeated. 我正在编写一个程序来查找重复给定字符串中的'a'数。 For example, the call findAmountA("aba", 7) means that it finds the number of 'a' in the string "aba" repeated for 7 characters. 例如,调用findAmountA(“aba”,7)意味着它找到重复7个字符的字符串“aba”中的“a”的数量。 So "abaabaa" is the final string, so that call would return 5. 因此“abaabaa”是最后一个字符串,因此该调用将返回5。

Without actually making the string 7 characters (so calls for 1,000,000 characters would not take so long), how would I use mathematics to accomplish this task? 如果没有实际使字符串成为7个字符(因此要求1,000,000个字符的时间不会那么长),我将如何使用数学来完成此任务? I cannot get further than this, as I have been trying to troubleshoot this for a while. 我不能比这更进一步,因为我一直试图解决这个问题。

Keep in mind I am a beginner Java programmer (Student) and do not want to use any advanced/fancy syntax that I would not learn in high school. 请记住,我是一名初学者Java程序员(学生),不想使用我在高中时不会学习的任何高级/花哨语法。 Thank you! 谢谢!

public class AInString {
    public static void main(String[] args) {
        boolean a = findAmountA("aba", 10) == 7;
        boolean b = findAmountA("a", 100) == 100;
        boolean c = findAmountA("abca", 10) == 5;
        boolean d = findAmountA("", 10) == 0;
        boolean e = findAmountA("abcaa", 1000000) == 600000;
        boolean f = findAmountA("abc", 0) == 0;
        boolean g = findAmountA("bcd", 10) == 0;
        System.out.println(a && b && c && d && e && f && g);
    }
    public static int findAmountA(String word, int n) {
        String s = word;
        if(s.length() == 0 || aInWord(word) == 0) {
            return 0;
        }else {
            int a = (aInWord(s));
            return a;
        }
    }
    public static int aInWord(String word) {
        String s = word;
        int aInWord = 0;
        for(int i = 0; i < word.length(); i++) {
            if(s.charAt(i) == 'a') {
                aInWord++;
            }
        }
        return aInWord;
    }

}

Let's say your short string w has N copies of 'a' in it. 假设您的短字符串w有N个副本'a' Then the result string will consist of K copies of w followed by a possibility empty “tail” string. 然后结果字符串将由w的K个副本组成,后跟可能的空“尾部”字符串。

The value of K can be determined by integer-dividing the number of 'a' s in the target string by N. Then the number t of 'a' s in the “tail” would be equal to the remainder of the division. K的值可以通过将目标串中的'a'的数量除以N来确定。然后,“尾部”中的“ 'a'的数量t将等于除法的余数。 Now you can print K copies of w followed by the shortest prefix of 'w' containing t 'a' s. 现在你可以打印w副本,然后打印包含t 'a'的最短前缀'w'

Now that you've counted the occurrences of a char a in a string word , you can count the occurrences of the char in the string extended n characters with: 现在您已经计算了字符串word char a的出现次数,您可以计算字符串扩展n字符中char的出现次数:

return n / word.length() * aInWord(word) + aInWord(word.substring(0, n % word.length()));

n / word.length() gives the number of full repeats of the string that fit into n . n / word.length()给出适合n的字符串的完整重复次数。 Multiplying this by the count of aInWord(word) gives the count of a in repeats of word that fit cleanly into n . 通过的计数乘以这个aInWord(word)给出的计数a在重复word适合干净成n

The rest is a matter of finding the number of repeats in the substring of word that doesn't fit cleanly into n using the % modulus operator to find the size of the partial substring (if any). 其余的是发现在的子串的重复的数量的问题word不干净适合n使用%模运算符以找到局部子串的大小(如果有的话)。 Adding the two counts together produces the total number of occurrences in the extended string. 将两个计数加在一起会产生扩展字符串中出现的总数。

Here is a clean version which avoids duplicate variables, extra conditionals and generalizes methods to maximize reusability: 这是一个干净的版本,它避免了重复变量,额外的条件和一般化方法来最大化可重用性:

class Main {
    public static void main(String[] args) {
        assert findAmount("aba", 10, "a") == 7;
        assert findAmount("a", 100, "a") == 100;
        assert findAmount("abca", 10, "a") == 5;
        assert findAmount("", 10, "a") == 0;
        assert findAmount("abcaa", 1000000, "a") == 600000;
        assert findAmount("abc", 0, "a") == 0;
        assert findAmount("bcd", 10, "a") == 0;
        System.out.println("tests passed");
    }

    public static int findAmount(String word, int n, String target) {
        if (word.length() == 0) {
            return 0;
        }

        return n / word.length() * count(target, word) + 
               count(target, word.substring(0, n % word.length()));
    }

    public static int count(String target, String s) {
        return s.length() - s.replace(target, "").length();
    }
}

Try it! 试试吧!

Divide the target length by the input length: for the example: 将目标长度除以输入长度:例如:

7 / 3 = 2 remainder 1

2 the number of "full copies" of the entire input string you will use. 2您将使用的整个输入字符串的“完整副本”数。 So, find the number of "a"s in the entire string, multiply by 2. 因此,在整个字符串中找到“a”的数量,乘以2。

You will take the first 1 character of the input to make up the remainder of the 7 characters. 您将获取输入的前1个字符以构成7个字符的剩余部分。 Count the number of "a"s in that substring. 计算该子字符串中“a”的数量。

Simply add these two numbers together. 只需将这两个数字加在一起即可

int total = count(input, "a") * targetLength / input.length()
          + count(input.substring(0, targetLength % input.length()), "a");

where count(input, c) is some method to count the number of occurrences of c in input . 其中count(input, c)是一些方法来计数的发生次数cinput

I made some changes in your code, take a look: 我对你的代码做了一些修改,看看:

public static void main(String[] args) {
    int a = findAmountA("aba", 10); // 7
    int b = findAmountA("a", 100); // 100;
    int c = findAmountA("abca", 10); //5;
    int d = findAmountA("", 10); //0;
    int f = findAmountA("abc", 0); //0;
    int g = findAmountA("bcd", 10); //0;
    System.out.println(a + " " + b + " " + c + " " + d + " " + f + " " + g);
}

public static int findAmountA(String word, int n) {
    if (word.length() < n) {
        for (int i=0; i<word.length(); i++) {
            while (word.length() < n) {
                word = word + word.charAt(i);
                break;
            }
        }
    } else if (word.length() > n) {
        for (int i=0; i<word.length(); i++) {
            word = word.substring(0, n);
        }
    } else {
        return aInWord(word);
    }
    return aInWord(word);
}

public static int aInWord(String word) {
    String s = word;
    int aInWord = 0;
    for(int i = 0; i < word.length(); i++) {
        if(s.charAt(i) == 'a') {
            aInWord++;
        }
    }

Thank you all for your help, using substrings I found an answer: 谢谢大家的帮助,使用子串我找到了答案:

public class AInString {
    public static void main(String[] args) {
        boolean a = findAmountA("aba", 10) == 7;
        boolean b = findAmountA("a", 100) == 100;
        boolean c = findAmountA("abca", 10) == 5;
        boolean d = findAmountA("", 10) == 0;
        boolean e = findAmountA("abcaa", 1000000) == 600000;
        boolean f = findAmountA("abc", 0) == 0;
        boolean g = findAmountA("bcd", 10) == 0;
        System.out.println(a && b && c && d && e && f && g);
    }
    public static int findAmountA(String word, int n) {
        String s = word;
        if(s.length() == 0 || aInWord(s) == 0) {
            return 0;
        }else {
            int a = aInWord(s)*(n/s.length());
            int b = n % s.length();
            return a + aInWord(s.substring(0, b));
        }
    }
    public static int aInWord(String word) {
        String s = word;
        int aInWord = 0;
        for(int i = 0; i < word.length(); i++) {
            if(s.charAt(i) == 'a') {
                aInWord++;
            }
        }
        return aInWord;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM