Large-capacity text word count (P1308 Los problem solution to a problem Valley, Java language description)

Questions asked

P1308 topic Link

Here Insert Picture Description
Here Insert Picture Description

analysis

This problem itself, then it is annoying that Italy, following analysis.

The title tag "high performance", and then look at the data range, violence matching certain death. I hate to use char [] slowly ink, Java operating this very annoying, so we strive to solve the problem now with a String! !

To do two things: First, get the number of times the word appears in numerous Pattern Text's word , the second is to obtain Pattern word first appeared in numerous position of the word in the Text .
Why I do not say that the matched text and match words to say, read on, you will understand.

If the matching string as it can indexOf String class () method, but not directly process count, but to open it while loop, and also easy to handle.

To this is also good, but unfortunately very sick, is not.
It is separated by whitespace text to N words, we want to match word and not a full text match, to train general solution can not solve the problem.

In this case our immediate thought might be split (), separated directly " “on the line, but can not.
Because, nausea that look prompted one test data, WA might just find that once any number of whitespace characters it ......
so use regular expressions ”\\s+"be segmented.

But ~ ~ so you pay up only 30 points, in fact, is simply wrong ideas, according to the acquired data sets 3, the statistics should be there from the beginning to the indexOf, straightforward thinking that is to say, do a indexOf, in counting slowly to chant, but unfortunately not right, because it went back to the text rather than matching word match.

Makes you bald head, ah, that continue pondering, we replaced the matching " " + pattern + " ", so as to ensure around with other word apart, that alone into words, I'm sorry, I still ah, because ignoring the beginning of the end of the special sentence.

这一加上对开头结尾的考虑,代码量激增,必须加很多变量去做过程的处理。具体的看最后的AC代码就行啦,为了调整整个不出Bug,比较麻烦,所以这题让人恶心。

比如我们要先用startWith(pattern + " “)做开头的判断,用endWith(” " + pattern)做结尾的判断,过程中怎么处理balabala,过程怎么进行跃进式的indexOf计数,如何保证第一次就是真的第一次,也会漏结尾的情况balabala,看代码就好了,多说无益。

还有那个“超大容量”,看后面我第二次提交的情况吧,那是什么鬼数据啊,我的记事本和Word都快炸了……好在只有测试数据9一个测试用例这样,不然岂不……太惨了吧……

此题比较恶心,不想多说了。

第一次提交——WA

Here Insert Picture Description

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String pattern = scanner.nextLine();
        String[] text_array = scanner.nextLine().split(" ");
        int counter = 0, index = -1;
        for (int i = 0; i < text_array.length; i++) {
            String s = text_array[i];
            if (s.equalsIgnoreCase(pattern)) {
                counter++;
                if (index == -1) {
                    index = i;
                }
            }
        }
        if (index == -1) {
            System.out.println(-1);
        } else {
            System.out.println(counter + " " + index);
        }
        scanner.close();
    }
}

错因前面分析了,就不说了,一看就知道的。

分享一下测试数据3:
in
u
tIXHUguyz PZYAJL BIv NAPoemaJ aTF LOvhV m s LSa n xDn mQnO T ettIq T AL fG B Xme t fct U tQ d

out
1 92

第二次提交——MLE

Here Insert Picture Description

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String pattern = scanner.nextLine().toUpperCase(), text = scanner.nextLine().toUpperCase();
        int length = pattern.length(), counter = 0, firstIndex = -1, tempIndex = -1;
        boolean endFlag = false;
        if (text.startsWith(pattern + " ")) {
            counter++;
            firstIndex = 0;
            pattern = " " + pattern + " ";
            tempIndex = text.indexOf(pattern);
        }
        if (text.endsWith(" " + pattern)) {
            counter++;
            endFlag = true;
        }
        pattern = " " + pattern + " ";
        if (counter == 0) {
            firstIndex = tempIndex = text.indexOf(pattern);
        }
        while (tempIndex != -1) {
            text = text.substring(tempIndex + length);
            tempIndex = text.indexOf(pattern);
            counter++;
        }
        if (firstIndex == -1) {
            if (endFlag) {
                System.out.println(1 + " " + (text.length()-1));
            } else {
                System.out.println(-1);
            }
        } else {
            System.out.println(counter + " " + (firstIndex+1));
        }
        scanner.close();
    }
}

这个测试数据9……我真没法Share,太大了,这就是我说的超大容量……
这样吧,我把文本数据导进了Word,我先进的电脑卡爆了,加载的数据:
Here Insert Picture Description

当然这在现在的海量文本数据里面微乎其微,但你要知道这只是一个简单的OJ题啊,这么大的数据量,用Java不得炸裂!!

但我们转而思考自身可能引发超时超内存的问题:数组!!
我们没开数组啊??但String本身不就有数组吗!!!
恍然大悟,我们开了太多substring()了啊,删去就好,换一个indexOf()的重载方法就好!!!

乌拉乌拉乌拉!!!

第三次提交——AC

Here Insert Picture Description

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String pattern = scanner.nextLine().toUpperCase(), text = scanner.nextLine().toUpperCase();
        int length = pattern.length(), counter = 0, firstIndex = -1, tempIndex = -1;
        boolean endFlag = false;
        if (text.startsWith(pattern + " ")) {
            counter++;
            firstIndex = 0;
            pattern = " " + pattern + " ";
            tempIndex = text.indexOf(pattern);
        }
        if (text.endsWith(" " + pattern)) {
            counter++;
            endFlag = true;
        }
        pattern = " " + pattern + " ";
        if (counter == 0) {
            firstIndex = tempIndex = text.indexOf(pattern);
        }
        while (tempIndex != -1) {
            tempIndex = text.indexOf(pattern, tempIndex+length);
            counter++;
        }
        if (firstIndex == -1) {
            if (endFlag) {
                System.out.println(1 + " " + (text.length()-1));
            } else {
                System.out.println(-1);
            }
        } else {
            System.out.println(counter + " " + (firstIndex+1));
        }
        scanner.close();
    }
}

总结

  • 在处理一个问题的时候,其实稍加变换解决思路就能大有改观。
  • 解决问题就是根据已知,不断转化转化再转化,直到转化出我们需要的条件,再求解。
  • Java的API博大精深,不断研究,总会有新收获呢。

不知道大家可有收获?今年是大年初一,新春快乐@All !!!

Published 367 original articles · won praise 626 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104084512