Help!

Help!

时间: 1ms        内存:128M

描述:

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

提示:

参考答案(内存最优[828]):

#include <string.h>
#include <stdio.h>
#include <ctype.h>

char a[1000], b[1000];

int na,nb,i,j,k,m,n,c;
char *pa, *pb, aa[100][100], bb[100][100];

int main(){

   scanf("%d ",&c);
   while (c--) {
      gets(a);
      gets(b);
      na = nb = 0;
      for (pa = strtok(a," ");pa;pa = strtok(NULL," ")){
         if (*pa == '<') *pa = '0';
         strcpy(aa[na++],pa);
      }
      for (pb = strtok(b," ");pb;pb = strtok(NULL," ")){
         if (*pb == '<') *pb = '1';
         strcpy(bb[nb++],pb);
      }
      if (na != nb) {
         printf("-\n");
         continue;
      }
      for (i=0;i<na;i++) {
         if (isdigit(aa[i][0])) {
           for (j=0;j<na;j++) if (!strcmp(bb[j],aa[i])) strcpy(bb[j],bb[i]);
           for (j=i+1;j<na;j++) if (!strcmp(aa[j],aa[i])) strcpy(aa[j],bb[i]);
           for (j=0;j<=i;j++) if (!strcmp(aa[j],aa[i])) strcpy(aa[j],bb[i]);
         } else if (isdigit(bb[i][0])) {
           for (j=0;j<na;j++) if (!strcmp(aa[j],bb[i])) strcpy(aa[j],aa[i]);
           for (j=i+1;j<na;j++) if (!strcmp(bb[j],bb[i])) strcpy(bb[j],aa[i]);
           for (j=0;j<=i;j++) if (!strcmp(bb[j],bb[i])) strcpy(bb[j],aa[i]);
         } else if (strcmp(aa[i],bb[i])){
            printf("-\n");
            goto dun;
         }
      }
      for (i=0;i<na;i++) {
         if (i) printf(" ");
         if (isdigit(aa[i][0])) printf("x"); else printf("%s",aa[i]);
      }
      printf("\n");
   dun:;}
   if (gets(a)) printf("Extra input!\n");
   return 0;
}

参考答案(时间最优[0]):

#include <string.h>
#include <stdio.h>
#include <ctype.h>

char a[1000], b[1000];

int na,nb,i,j,k,m,n,c;
char *pa, *pb, aa[100][100], bb[100][100];

int main(){

   scanf("%d ",&c);
   while (c--) {
      gets(a);
      gets(b);
      na = nb = 0;
      for (pa = strtok(a," ");pa;pa = strtok(NULL," ")){
         if (*pa == '<') *pa = '0';
         strcpy(aa[na++],pa);
      }
      for (pb = strtok(b," ");pb;pb = strtok(NULL," ")){
         if (*pb == '<') *pb = '1';
         strcpy(bb[nb++],pb);
      }
      if (na != nb) {
         printf("-\n");
         continue;
      }
      for (i=0;i<na;i++) {
         if (isdigit(aa[i][0])) {
           for (j=0;j<na;j++) if (!strcmp(bb[j],aa[i])) strcpy(bb[j],bb[i]);
           for (j=i+1;j<na;j++) if (!strcmp(aa[j],aa[i])) strcpy(aa[j],bb[i]);
           for (j=0;j<=i;j++) if (!strcmp(aa[j],aa[i])) strcpy(aa[j],bb[i]);
         } else if (isdigit(bb[i][0])) {
           for (j=0;j<na;j++) if (!strcmp(aa[j],bb[i])) strcpy(aa[j],aa[i]);
           for (j=i+1;j<na;j++) if (!strcmp(bb[j],bb[i])) strcpy(bb[j],aa[i]);
           for (j=0;j<=i;j++) if (!strcmp(bb[j],bb[i])) strcpy(bb[j],aa[i]);
         } else if (strcmp(aa[i],bb[i])){
            printf("-\n");
            goto dun;
         }
      }
      for (i=0;i<na;i++) {
         if (i) printf(" ");
         if (isdigit(aa[i][0])) printf("x"); else printf("%s",aa[i]);
      }
      printf("\n");
   dun:;}
   if (gets(a)) printf("Extra input!\n");
   return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

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