求大佬捞只有20分的菜鸡一波······

回复帖子

@HeavyTiger 2019-10-10 00:36 回复

大佬们,谁来帮帮一个只有20分打算自暴自弃的萌新····呜呜呜,我可怎么办啊~~~

有解决办法吗,可能我没搞懂题?把题看漏了要求第一次出现的地方,然后用字符串做了,之后一脸懵逼,这还能捞一波么。。。。。。。

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

void function(char *pi){
    int i=0;
    while(*(pi+i)!='\0'){
        if(*(pi+i)<=90 && *(pi+i)>=65){
            *(pi+i) = *(pi+i)+32;
        }
        i++;
    }
}

char word[11];

int main(){
    char word_compare[11];
    scanf("%s",&word);  
    for(int i=0;i<strlen(word);i++){
        if(word[i]<=90 && word[i]>=65){
            word[i] = word[i]+32;
        }
    }
    char *pi=&word_compare[0];
    int sum=0,ok=0,num=0,b;
    while((scanf("%s",&word_compare))!=EOF){
            function(pi);
            if(ok==0&&strcmp(word,word_compare)==0){
                b=num;  
                ok=1;
            }
            num += (strlen(word_compare)+1);
            if(strcmp(word,word_compare)==0){

                sum++;
            }
        }
    if(sum==0){
        printf("-1");
    }
    else printf("%d %d",sum,b); 
    return 0;
}
@From 2019-10-14 20:38 回复 举报

@与我常在 别说了我KMP挂掉了我太蒟蒻了

#include <cstdio>
#include <cctype>
#include <climits>
#include <cstring>  
#include <algorithm>
#include <string>

using namespace std;

const int MAXN = 15;

int ans1, ans2 = INT_MAX;
int nxt[MAXN];
string str1, str2;

string getStr() {
    char ch = getchar();
    string str = string();
    while (!isalpha(ch));
    while (isalpha(ch)) str.push_back(ch), ch = getchar();
    return str;
}

string getText() {
    char ch = getchar();
    string str = string();
    while (!isalpha(ch));
    while (isalpha(ch) || ch == ' ') str.push_back(ch), ch = getchar();
    return str;
}

void preSolve(string &str1, string &str2) {
    for (int i = 0; i < str1.length(); ++i) {
        str1[i] = tolower(str1[i]);
    }
    for (int i = 0; i < str2.length(); ++i) {
        str2[i] = tolower(str2[i]);
    }
}

void getFail(string str) {
    int j = -1;
    memset(nxt, -1, sizeof(nxt));
    for (int i = 1; i < str.length(); ++i) {
        while (j != -1 && str[j + 1] != str[i]) j = nxt[j];
        if (str[j + 1] == str[i]) j += 1;
        nxt[i] = j;
    }
}

void KMP(string str1, string str2) {
    getFail(str1);
    int j = -1;
    for (int i = 0; i < str2.length(); ++i) {
        while (j != -1 && str1[j + 1] != str2[i]) j = nxt[j];
        if (str1[j + 1] == str2[i]) j += 1;
        if (j == str1.length() - 1) {
            ans1 += 1;
            ans2 = min(ans2, i - j);
            j = nxt[j];
        }
    }
}

int main() {
    str1 = " " + getStr() + " ";
    str2 = " " + getText() + " ";
    preSolve(str1, str2);
    KMP(str1, str2);
    if (!ans1) puts("-1");
    else printf("%d %d", ans1, ans2);
    return 0;
}
@与我常在 2019-10-14 21:04 回复 举报

@From 我暴力匹配

#include <cstdio>
#include <cstring>
const int N = 1000010;
char a[15], b[N];
int p[N];
int main() {
    int tot = 0, flag = 1, wz;
    scanf("%s",a + 1);
    getchar();
    gets(b + 1);
    int l1 = strlen(a + 1), l2 = strlen(b + 1);
    int j = 0;
    for(int i = 1; i < l1;i ++ ) {
        while(j > 0 && a[i + 1] != a[j + 1]) j = p[j];
        if(a[i + 1] == a[j + 1]) j ++ ;
        p[i + 1] = j;
    }
    j = 0;
    for(int i = 0;i < l2;i ++ ) {
        while(j > 0 && b[i + 1] != a[j + 1] && (b[i + 1] - 'A' != a[j + 1] - 'a') && (b[i + 1] - 'a' != a[j + 1] - 'A')) j = p[j];
        if(b[i + 1] == a[j + 1] || (b[i + 1] - 'A' == a[j + 1] - 'a') || (b[i + 1] - 'a' == a[j + 1] - 'A')) j ++ ;
        if(j == l1 && (b[i - l1 + 1] == ' ' || !(i - l1 +1)) && (b[i + 2] == ' ' || i + 1 == l2)) {
            tot ++;
            j = 0;
            if(flag)
                wz = i - l1 + 1, flag = 0;
        }
    }
    if(!tot) puts("-1");
    else printf("%d %d", tot, wz);
    return 0;
}
反馈
如果你认为某个帖子有问题,欢迎向洛谷反馈,以帮助更多的同学。



请具体说明理由,以增加反馈的可信度。