首页 > 其他 > 详细

HDU-2594-Simpsons’ Hidden Talents

时间:2019-02-13 13:20:06      阅读:197      评论:0      收藏:0      [点我收藏+]
  • kmp
    Accepted 2594 0MS 1484K 671 B G++
    #include "cstdio"
    using namespace std;
    const int MAXN = 50005;
    char s1[MAXN], s2[MAXN];
    int next[MAXN] = {-1};
    void getNext(char* s) {
        int i = 0, j = -1;
        while (s[i]) {
            if (j == -1 || s[i] == s[j]) {
                next[++i] = ++j;
            } else {
                j = next[j];
            }
        }
    }
    int kmp(char* s1, char* s2) {
        int i = 0, j = 0;
        while (s1[i]) {
            if (j == -1 || s1[i] == s2[j]) {
                j++;
                i++; 
            } else {
                j = next[j];
            }
        }
        return j;
    }
    void print(int n) {
        if (n == 0) {
            puts("0");
            return;
        }
        s1[n] = \0;
        printf("%s %d\n", s1, n);
    }
    int main() {
        while (gets(s1)) {
            gets(s2);
            getNext(s1);
            print(kmp(s2, s1));    
        }
        return 0;
    } 

     

HDU-2594-Simpsons’ Hidden Talents

原文:https://www.cnblogs.com/Angel-Demon/p/10369234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!