1 条题解
-
0
C++ :
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct node { char s[30]; int dep; } list1[5010],list2[5010]; char a[7][30],b[7][30]; int n; bool check(char *s1,char *s2) { if (strlen(s1)!=strlen(s2)) return false; for (int i=0;i<strlen(s1);i++) if (s1[i]!=s2[i]) return false; return true; } bool pan1(char *s,int i,int x) { for (int j=i;j<i+strlen(a[x]);j++) if (s[j]!=a[x][j-i]) return false; return true; } bool pan2(char *s,int i,int x) { for (int j=i;j<i+strlen(b[x]);j++) if (s[j]!=b[x][j-i]) return false; return true; } void bfs() { int head1,tail1,head2,tail2,i,j,k,l; head1=tail1=head2=tail2=1; while (head1<=tail1 && head2<=tail2) { if (list1[head1].dep+list2[head2].dep>10) { printf("NO ANSWER!\n"); return ; } for ( i=0;i<strlen(list1[head1].s);i++) for ( j=1;j<=n;j++) if (pan1(list1[head1].s,i,j)) { tail1++; for (k=0;k<i;k++) list1[tail1].s[k]=list1[head1].s[k]; for (l=0;l<strlen(b[j]);l++,k++) list1[tail1].s[k]=b[j][l]; for (l=i+strlen(a[j]);l<=strlen(list1[head1].s);l++,k++) list1[tail1].s[k]=list1[head1].s[l]; list1[tail1].s[k]='\0'; list1[tail1].dep=list1[head1].dep+1; for (k=1;k<=tail2;k++) if (check(list1[tail1].s,list2[k].s)) { printf("%d\n",list1[tail1].dep+list2[k].dep); return ; } } for ( i=0;i<strlen(list2[head2].s);i++) for ( j=1;j<=n;j++) if (pan2(list2[head2].s,i,j)) { tail2++; for (k=0;k<i;k++) list2[tail2].s[k]=list2[head2].s[k]; for ( l=0;l<strlen(a[j]);l++,k++) list2[tail2].s[k]=a[j][l]; for (l=i+strlen(b[j]);l<=strlen(list2[head2].s);l++,k++) list2[tail2].s[k]=list2[head2].s[l]; list2[tail2].s[k]='\0'; list2[tail2].dep=list2[head2].dep+1; for (k=1;k<=tail1;k++) if (check(list1[k].s,list2[tail2].s)) { printf("%d\n",list1[k].dep+list2[tail2].dep); return ; } } head1++; head2++; } printf("NO ANSWER!\n"); } int main() { scanf("%s%s",list1[1].s,list2[1].s); n=1; while (scanf("%s%s",a[n],b[n])!=EOF) n++; n--; list1[1].dep=list2[1].dep=0; bfs(); return 0; }
Pascal :
const maxn=2300; type node=record str:string; dep:longint; end; ctype=array[1..maxn]of ^node; bin=0..1; var maxk:byte;c:array [0..1]of ctype; x0:array[0..6,0..1]of string; open,guan:array [0..1] of longint; procedure init; var temp:string; i,j:longint; begin for i:=0 to 1 do for j:=1 to maxn do new(c[i,j]); i:=0; while not eof and (i<=6) do begin readln(temp); x0[i,0]:=copy(temp,1,pos(' ',temp)-1); x0[i,1]:=copy(temp,pos(' ',temp)+1,length(temp)); inc(i); end; maxk:=i-1; end; procedure calc; var st:bin; d:string; procedure bool(st:bin); var i:longint; begin if x0[0,1-st]=c[st,guan[st]]^.str then begin writeln(c[st,guan[st]]^.dep); halt; end; for i:=1 to guan[1-st] do if c[st,guan[st]]^.str=c[1-st,i]^.str then begin writeln(c[st,guan[st]]^.dep+c[1-st,i]^.dep); halt; end; end; procedure checkup(st:bin); var i:longint; begin for i:=1 to guan[st]-1 do if c[st,i]^.str=c[st,guan[st]]^.str then begin dec(guan[st]); exit; end; bool(st); end; procedure expand(st:bin); var i,j,k,lx,ld:longint; begin inc(open[st]); d:=c[st,open[st]]^.str; k:=c[st,open[st]]^.dep; ld:=length(d); for i:=1 to maxk do begin lx:=length(x0[i,st]); for j:=1 to ld do begin if (copy(d,j,lx)=x0[i,st]) and (length(copy(d,1,j-1)+x0[i,1-st]+copy(d,j+lx,ld))<=115) then begin if guan[st]>=maxn then exit; inc(guan[st]); c[st,guan[st]]^.str:=copy(d,1,j-1)+x0[i,1-st]+copy(d,j+lx,ld); c[st,guan[st]]^.dep:=k+1; checkup(st); end; end; end; end; begin for st:=0 to 1 do begin open[st]:=0; guan[st]:=1; c[st,guan[st]]^.str:=x0[0,st]; c[st,guan[st]]^.dep:=0; bool(st); end; repeat if (open[0]<=open[1]) and not ((open[0]>=guan[0]) or (guan[0]>=maxn) or (c[0,guan[0]]^.dep>10)) then expand(0); if (open[1]<=open[0]) and not ((open[1]>=guan[1]) or (guan[1]>=maxn) or (c[1,guan[1]]^.dep>10)) then expand(1); if not ((open[0]>=guan[0]) or (guan[0]>=maxn) or (c[0,guan[0]]^.dep>10)) then expand(0); if not ((open[1]>=guan[1]) or (guan[1]>=maxn) or (c[1,guan[1]]^.dep>10)) then expand(1); until (open[0]>=guan[0]) or (c[0,guan[0]]^.dep>10) or (guan[0]>=maxn) and (guan[1]>=maxn) or (open[1]>=guan[1]) or (c[1,guan[1]]^.dep>10); end; begin init; calc; writeln('NO ANSWER!'); end.
- 1
信息
- ID
- 219
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者