zoukankan      html  css  js  c++  java
  • EOJ382 Match Maker

    EOJ382 Match Maker

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submits: 50   Accepted: 6

    Description

    In Computer Science, pattern matching is the act of checking if a certain sequence conforms (matches) a given pattern. Patterns are usually specified using a language based on regular expression. In this problem, we'll use a simple regular expression to express patterns on sequences of decimal digits. A pattern is a sequence of one or more decimal digits `0' ...`9', asterisks `*', and hash signs `#'. A `*' denotes a sequence of an even number of digits, whereas a `#' denotes a sequence of an odd number of digits. For example, the pattern ``129" only matches the sequence 129. The pattern ``1*3" matches all sequences beginning with 1, ending with 3, and having an even number of decimal digits between the first and last digits. As another example, the pattern ``#55" matches the sequences 155, 12355, 1234555, but none of the sequences 55, 1255, 123455. Your task is to write a program to find if a given sequence matches a given pattern.

    Input

    Your program will be tested on one or more data sets. Each data set contains a single pattern and one or more sequences to match. The first line of each data set specifies the pattern, and the remaining lines specify the sequences to match against that pattern. The end of a data set (except the last) is identified by the word ``END" (without the double quotes.) The end of the last data set is identified by the word ``QUIT". All lines are 100,000 characters long or shorter.

    Output

    k.s. result 


    Where k is the test case number (starting at one,) and s is the sequence number (starting at one within each test case,) and result is either the word ``match" if the given string matches the pattern, or the word ``not" if it doesn't.

    Sample Input

    129 
    1299 
    129 
    1129 
    END 
    1*3 
    123 
    1223 
    END 
    #55 
    155 
    12355 
    55 
    1255 
    QUIT

    Sample Output

    1.1. not 
    1.2. match 
    1.3. not 
    2.1. not 
    2.2. match 
    3.1. match 
    3.2. match 
    3.3. not 
    3.4. not
    *************************************************
    题目大意:字符串匹配,一个含有数字、'#'、'*'的模式串去匹配一个纯数字的字符串,一个'#'可以匹配奇数个数字,一个'*'可以匹配偶数个数字,如果可以匹配就输出match否则就输出not。
    解题思路:根据'#'、'*'把模式串分成几个子模式串进行KMP。
    以下是我的代码,又长又搓,哎。。
    #include <stdio.h>
    #include <string.h>
    #define N 100005
    
    char a[N],b[N],c[N];
    int ed,lf[N],p[N],l1,l2,s[N],e[N],top;
    
    int solve(int s,int e,int kf)
    {
        int j=-1;
        for(int i=ed+1;i<l2;i++)
        {
            while(1)
            {
                if(a[j+1+s]==b[i])
                {
                    j++;
                    if(j+s+1==e)
                    {
                        if(((i-ed-e+s)&1)==kf)
                        {
                            ed=i;
                            return 1;
                        }
                        else
                        {
                            if(j==0)break;
                            j--;
                            j=p[j+s];
                            continue;
                        }
                    }
                    break;
                }
                if(j==-1)break;
                j=p[j+s];
            }
        }
        return 0;
    }
    
    void run(void)
    {
        int t1=0,t2=0;
        while(1)
        {
            if(!t2)
            {
                t1++;t2=1;
                scanf("%s",a+1);
                if(a[1]=='Q')return ;
                if(a[1]=='E')
                {
                    t2=0;
                    continue;
                }
                a[0]='0';
                l1=strlen(a);
                a[l1++]='0';
                memset(lf,0,sizeof(lf));
                memset(p,-1,sizeof(p));
                top=0;
                int i=0;
                for(i=0;i<l1&&a[i]>='0'&&a[i]<='9';i++);
                int flag=0;
                for(i;i<l1;i++)
                {
                    if(a[i]>='0'&&a[i]<='9')
                    {
                        if(!flag)
                            s[++top]=i;
                        flag=1;
                    }
                    else
                    {
                        if(flag)
                        {
                            e[top]=i;
                            for(int i=s[top]+1;i<e[top];i++)
                            {
                                int k=p[i-1];
                                while(1)
                                {
                                    if(a[k+1+s[top]]==a[i])
                                    {
                                        p[i]=k+1;
                                        break;
                                    }
                                    if(k==-1)break;
                                    k=p[k+s[top]];
                                }
                            }
                        }
                        flag=0;
                        if(a[i]=='#')
                            lf[top+1]^=1;
                    }
                }
                if(top)e[top]=l1;
                continue;
            }
            else
            {
                t2++;
                scanf("%s",b+1);
                if(b[1]=='Q')return ;
                if(b[1]=='E')
                {
                    t2=0;
                    continue;
                }
                b[0]='0';
                l2=strlen(b);
                b[l2++]='0';
            }
            int i,flag=0;
            for(i=0;i<l1&&a[i]>='0'&&a[i]<='9'&&!flag;i++)
                if(a[i]!=b[i])
                    flag=1;
            if(flag||(i==l1&&i<l2))
            {
                printf("%d.%d. not\n",t1,t2-1);
                continue;
            }
            if(i==l1&&i==l2)
            {
                printf("%d.%d. match\n",t1,t2-1);
                continue;
            }
            ed=i-1;
            flag=0;
            int mark=0;
            for(int i=1;i<top&&!mark;i++)
                if(solve(s[i],e[i],lf[i])==0)
                    mark=1;
            if(mark)
            {
                printf("%d.%d. not\n",t1,t2-1);
                continue;
            }
            int k=l2-1;
            for(int j=l1-1;j>=0&&a[j]>='0'&&a[j]<='9';j--)
                if(a[j]!=b[k--])
                    mark=1;
            if(mark||k<ed||((k-ed)&1)!=lf[top])
            {
                printf("%d.%d. not\n",t1,t2-1);
                continue;
            }
            printf("%d.%d. match\n",t1,t2-1);
        }
    }
    
    int main()
    {
        run();
        return 0;
    }
    

      

  • 相关阅读:
    MVC ORM 架构
    Kubernetes 第八章 Pod 控制器
    Kubernetes 第七章 Configure Liveness and Readiness Probes
    Kubernetes 第六章 pod 资源对象
    Kubernetes 第五章 YAML
    Kubernetes 核心组件
    Kubernetes 架构原理
    Kubernetes 第四章 kubectl
    Kubernetes 第三章 kubeadm
    yum 配置及yum 源配置
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2279455.html
Copyright © 2011-2022 走看看