Codeup1000000634 question B: P2 count words

Title Description:

Count the number of words (stat.cpp/c/pas)
General text editors have the function of finding words. This function can quickly locate the position of specific words in the article, and some can count the times of specific words in the article.

Now, please program to realize this function. The specific requirements are: given a word, please output the number of times it appears in a given article and the position of its first appearance. Note: when matching words, it is not case sensitive, but it is required to match exactly, that is, the word must be exactly the same as an independent incomplete in the article without case sensitivity (see example 1). If a given word is only a part of a word in the article, it is not a match (see example 2).

Input:

The input file has 2 lines in total.

The first line is a string containing only letters, representing a given word;

Line 2 is a string that may contain only letters and spaces to represent a given article.

Output:

There is only one line. If a given word is found in the article, two integers are output, separated by a space, They are the number of times the word appears in the article and the position of the first occurrence (that is, the position of the first letter of the word in the article when it appears for the first time in the article, starting from 0); if the word does not appear in the article, an integer - 1 is directly output.

Sample input:

Input example 1:

To
to be or not to be is a question

Input example 2:

to
Did the Ottoman Empire lose its power at that time

Sample output:

Output example 1:

2 0

Output example 2:

-1

Tips:

1 ≤ single Words long degree ≤ 10 1 \ leword length \ le10 1 ≤ word length ≤ 10.

1 ≤ writing chapter long degree ≤ 10 , 000 , 000 . 1 \ learticle length \ le10000000. 1 ≤ article length ≤ 10000000.

Implementation code 1:

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;

void strLower(string &s) {
    for(int i = 0; i < s.size(); i++) {
        if(s[i] >= 'A' && s[i] <= 'Z') {
            s[i] = s[i] - 'A' + 'a';
        }

    }
}

int main() {
    string text;
    string pattern;
    string sub;
    while(getline(cin, pattern)) {
        getline(cin, text);
        strLower(pattern);
        strLower(text);
        int l = 0;
        int ans = 0;
        int index;
        int n = text.size();
        for(int i = 0; i < n; i++) {
            if(text[i] == ' ' || i == n - 1) {
                if(text[i] == ' ') {
                    sub = text.substr(l, i - l);
                } else {
                    sub = text.substr(l, i - l + 1);
                }
                if(sub == pattern) {
                    ans++;
                    if(ans == 1) {
                        index = l;
                    }
                }
                l = i + 1;
            }
        }
        if(ans == 0) {
            printf("-1\n");
        } else {
            printf("%d %d\n", ans, index);
        }
    }
    return 0;
}

Implementation code 2:

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;

const int maxn = 20;
int nextval[maxn];

void getNextval(string s) {
    int j = -1;
    nextval[0] = -1;
    for(int i = 1; i < s.size(); i++) {
        while(j != -1 && s[i] != s[j + 1]) {
            j = nextval[j];
        }
        if(s[i] == s[j + 1]) {
            j++;
        }
        if(j == -1 || s[i + 1] != s[j + 1]) {
            nextval[i] = j;
        } else {
            nextval[i] = nextval[j];
        }
    }
}

int KMP(string text, string pattern, int &index) {
    int n = text.size();
    int m = pattern.size();
    getNextval(pattern);
    int ans = 0, j = -1;
    for(int i = 0; i < n; i++) {
        while(j != -1 && text[i] != pattern[j + 1]) {
            j = nextval[j];
        }
        if(text[i] == pattern[j + 1]) {
            j++;
        }
        if(j == m - 1) {
            //  Consider three situations: the beginning, the end, the middle and the whole word
            if(i - m >= 0 && i < n - 1 && text[i + 1] == ' ' && text[i - m] == ' ' || i - m + 1 == 0 && i < n - 1 && text[i + 1] == ' ' || i - m >= 0 && i == n - 1 && text[i - m] == ' ' || i - m + 1 == 0 && i == n - 1) {
                ans++;
                if(ans == 1) {
                    index = i - m + 1;
                }
            }
            j = nextval[j];
        }
    }
    return ans;
}

void strLower(string &s) {
    for(int i = 0; i < s.size(); i++) {
        if(s[i] >= 'A' && s[i] <= 'Z') {
            s[i] = s[i] - 'A' + 'a';
        }
    }
}

int main() {
    string pattern;
    string text;
    while(getline(cin, pattern)) {
        getline(cin, text);
        strLower(text);
        strLower(pattern);
        int index = 0;
        int num =  KMP(text, pattern, index);
        if(num == 0) {
            printf("-1\n");
        } else {
            printf("%d %d\n", num, index);
        }
    }
    return 0;
}

Keywords: C C++ Algorithm string KMP

Added by Lars Berg on Tue, 04 Jan 2022 11:56:05 +0200