• HDU 5311 Hidden String


    我的方法是将题目中给的那个串(anniversary)拆分成三段。枚举所有情况,也就100多种的样子。

    每一种情况去判断在输入的串中能不能找到这三个串,并且不相交。

    o(︶︿︶)o 唉,思路很快就有了,代码写得丑,找错找了半天,最终在场外Submit的1A了。

    写题时候脑瘫,还搞了个KMP上去。。。其实长度为100的串,暴力和KMP差距并不是很大,似乎都能 0ms AC。

    按照自己的思路来能过这题,已经很满足了。。。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    char s[1000];
    char y[100]="anniversary";
    char one[100],two[100],three[100];
    char R[1000];
    
    const int N = 1000002;
    int Next[N];
    char S[N], T[N];
    int slen, tlen;
    int tot;
    int flag;
    int lenZ;
    
    void getNext()
    {
        int j, k;
        j = 0; k = -1; Next[0] = -1;
        while(j < tlen)
            if(k == -1 || T[j] == T[k])
                Next[++j] = ++k;
            else
                k = Next[k];
    
    }
    
    int KMP_Index()
    {
        int i = 0, j = 0;
        getNext();
    
        while(i < slen && j < tlen)
        {
            if(j == -1 || S[i] == T[j])
            {
                i++; j++;
            }
            else
                j = Next[j];
        }
        if(j == tlen)
            return i - tlen;
        else
            return -1;
    }
    
    int main()
    {
        int TT;
        int i,j,k;
        scanf("%d",&TT);
        while(TT--)
        {
            scanf("%s",s);
            strcpy(R,s);
            lenZ=strlen(s);
            flag=0;
            int i,j;
            for(i=1;i<=10;i++)
            {
                for(j=i;j<=9;j++)
                {
                    strcpy(s,R);
    
                    int u=0;
                    for(k=0;k<=i-1;k++) one[u]=y[k],u++;
                    one[u]='';
                    u=0;
    
                    for(k=i;k<=j;k++) two[u]=y[k],u++;
                    two[u]='';
                    u=0;
    
                    for(k=j+1;k<11;k++) three[u]=y[k],u++;
                    three[u]='';
                    u=0;
    
                    //printf("%s %s %s
    ",one,two,three);
                    if(i==1&&j==2)
                    {
                        strcpy(one,"anniv");
                        strcpy(two,"ers");
                        strcpy(three,"ary");
                    }
    
                    int lenone=strlen(one);
                    int lentwo=strlen(two);
                    int lenthree=strlen(three);
    
                    strcpy(S,s);
                    strcpy(T,one);
                    slen = strlen(S);
                    tlen = strlen(T);
    
                    int oneshou=KMP_Index();
    
    
                    int ii;
                    if(oneshou!=-1)
                    {
                        S[0]=0;
                        tot=0;
                        lenZ=strlen(s);
                        for(ii=oneshou+lenone;ii<lenZ;ii++)
                            S[tot]=s[ii],tot++;
                        S[tot]='';
    
                        strcpy(T,two);
    
                        slen = strlen(S);
                        tlen = strlen(T);
                        int twoshou=KMP_Index();
    
                        if(twoshou!=-1)
                        {
                            strcpy(s,S);
                            lenZ=strlen(S);
                            tot=0;
                            S[0]=0;
                            for(ii=twoshou+lentwo;ii<lenZ;ii++)
                                S[tot]=s[ii],tot++;
                            S[tot]='';
                            strcpy(T,three);
                            slen = strlen(S);
                            tlen = strlen(T);
                            if(KMP_Index()!=-1)
                                flag=1;
                        }
                    }
                    if(flag) break;
                }
                if(flag) break;
            }
            if(flag) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    Android 数据存储四种方式
    Android 文件系统路径
    Android 修改R包名方法
    Android startActivity 页面跳转
    Android 硬件加速器及其问题小结 gif不显示
    Android 用户界面 ViewGroup(FrameLayout LinearLayout AbsoluteLayout RelativeLayout TableLayout)
    Android 中解决输入法挡住输入框的问题
    Android 调用系统自带应用
    Android onMeasure(widthSpec, heightSpec)
    Android Bitmap Caching Bitmaps(渣翻译)
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4677120.html
Copyright © 2020-2023  润新知