您的位置:首页 > 大数据 > 人工智能

Clairewd’s message

2015-07-05 21:47 429 查看

http://acm.hdu.edu.cn/showproblem.php?pid=4300

#include <iostream> 

#include <cstdio> 

#include <string.h> 

using namespace std; 

int next1[100005]; 

char str[27]; 

char s1[100005],s2[100005]; 

void getnext(char *t) 



    int i = 0,j = -1; 

    next1[0] = -1; 

    while(t[i]) 

    { 

        if(j == -1 || t[i] == t[j]) 

        { 

            i++; 

            j++; 

            next1[i] = j; 

        } 

        else 

            j = next1[j]; 

    } 



int kmp(char *s,char *t) 



    int i = 0,j = 0; 

    int slen =strlen(s),tlen = strlen(t); 

    getnext(t); 

    while(j<tlen) 

    { 

        if(j == -1 || s[i] == t[j]) // tÊÇÃ÷ÎļÓÂÒÂ룬sÊǺóÃæÒ»°ëµÄÃ÷ÎÄ

        { 

            i++; 

            j++; 

            if(i == slen) 

                return j; 

        } 

        else 

            j = next1[j]; 

    } 

    return 0; 



int main() 



// freopen("C:\\Users\\John\\Desktop\\hi.txt","r",stdin);

    int t,i; 

    cin >> t; 

    while(t--) 

    { 

        scanf("%s",str); 

        scanf("%s",s1); 

        int len = strlen(s1); 

        strcpy(s2,s1+(len+1)/2); 

        printf("%s",s1); 

        for(i = 0; s1[i]; i++) 

        { 

            for(int j = 0; j<26; j++) 

            { 

                if(s1[i] == str[j]) 

                { 

                    s1[i] = 'a'+j; 

                    break; 

                } 

            } 

        } 

        int flag = kmp(s2,s1);  //s1ÊÇÃ÷ÎļÓÂÒÂ룬s2ÊǺóÃæÒ»°ëµÄÃ÷ÎÄ

        for(i = flag; i<len-flag; i++) 

        { 

            printf("%c",s1[i]); 

        } 

        cout << endl; 

    } 

    return 0; 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: