若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1180: Help!

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,035

题目描述

MegaFirm Inc. has created a set of patterns to aid its telephone help-desk operators in responding to customers. A pattern is a phrase consisting of words and placeholders. A word is simply a string of letters. A placeholder is a word enclosed in angle brackets (that is < … >). A phrase matches a pattern if each placeholder in the pattern can be systematically replaced by a word so as to make the pattern and phrase equal. By "systematically replaced" we mean that all placeholders enclosing the same word are replaced by the same word.
For example, the phrase

to be or not to be

matches the pattern

<foo> be <bar> not <foo> <baf>

because we can replace <foo> by to, <bar> by or, and <baf> by be.
Given two patterns, you are to find a phrase that matches both.
The first line of input contains n, the number of test cases. Each test case consists of two lines of input; each a pattern. Patterns consist of lowercase words, and placeholders containing lowercase words. No pattern exceeds 100 characters. Words contain at most 16 characters. A single space separates adjacent words and placeholders.
For each test case, output a phrase that matches both patterns. If several phrases match, any will do. If no phrase matches, output a line containing "-" (a single minus sign).

输入

暂无

输出

暂无

样例输入

3
how now brown 
 now  cow
who are you
  
 b
c 

样例输出

how now brown cow
-
c b

参考代码

#include <stdio.h>
#include <string.h>
char a[1000], b[1000];
int t,i,j;
struct nn 
{
    int n;
    char lbl;
    struct nn *fwd[100];
    int fwdi[100];
}
n[200], *p, *root1, *root2;
char * parse(int depth,char *x, int ni, struct nn **r) 
{
    char lbl = *x++;
    struct nn *pp = p++;
    int i;
    pp->n=ni;
    pp->lbl = lbl;
    if (*x == '(') 
    {
        while (*x == ',' || *x == '(') 
        {
            x = parse(depth+1,++x,1,&pp->fwd[pp->n]);
            pp->fwdi[pp->n] = 0;
            pp->fwd[pp->n]->fwd[0] = pp;
            pp->fwd[pp->n]->fwdi[0] = pp->n;
            pp->n++;
        }
        x++;
    }
    *r = pp;
    return x;
}
comp(struct nn *t, struct nn *u, int w, int x) 
{
    int i,j;
    if (t->n != u->n | t->lbl != u->lbl) return 0;
    if (w == -1) return comp(t->fwd[0],u->fwd[0],t->fwdi[0],u->fwdi[0]);
    j = (x+1)%u->n;
    for (i=(w+1)%t->n;i!=w;i=(i+1)%t->n) 
    {
        if (!comp(t->fwd[i],u->fwd[j],t->fwdi[i],u->fwdi[j])) return 0;
        j = (j+1)%u->n;
    }
    return 1;
}
equiv(struct nn *t, int where,int depth) 
{
    int i;
    if (t->n == 1 && comp(t,root1,-1,-1)) return 1;
    if (where == -1) 
    {
        for (i=0;i<t->n;i++) 
        {
            if(equiv(t->fwd[i],t->fwdi[i],depth+1)) return 1;
        }
    } else 
    {
        for (i=(where+1)%t->n;i!=where;i=(i+1)%t->n) if(equiv(t->fwd[i],t->fwdi[i],depth+1)) return 1;
    }
    return 0;
}
main() 
{
    scanf("%d ",&t);
    while (t--) 
    {
        p = n;
        if (!gets(a)) 
        {
            printf("short inputn"); exit(1); }
      parse(0,a,0,&root1);
      while(root1->n > 1)root1=root1->fwd[1];
      if (!gets(b)) {printf("short inputn"); exit(1); }
      parse(0,b,0,&root2);
      if(!strcmp(a,b) || equiv(root2,-1,0)) printf("samen");else printf("differentn");
   }
   if (gets(a)) printf("extraneous input!n");
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Skip Dreams孤独患者 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist