match

来源:互联网 发布:photogragh是什么软件 编辑:程序博客网 时间:2024/04/28 14:42

题目描述

      小x在解说F7决赛时的搭档是韩乔生,以至于小x没有任何能说上话的机会。无聊的他玩起了填字游戏。一个3*3的九宫格里,每个格子里都被填上了一个字母,从而我们得到了6个单词。现在,小x随手写了6个单词,他想让你帮他找到一种填字母的方案,使得这6个单词都出现在了九宫格里。

输入

共六行,每行一个长度为3的单词(全部大写)。

输出

如果找不到方案,输出“0”(不包含引号)

如果能找到,输出包含3行,第i行对应九宫格的第i行(最后一行行末要换行)。

如果有多种方案,请输出每种方案对应的字符串中字典序最前的一种(将行与行首尾相连,就可以得到一个字符串)。

样例输入

Input1ANAANADARDARRADRADInput2EVOHEPHIRIVAPADRODInput3AKOCESDOCDONESIKES
var        a,b,c,i,j,k,i1,j1,ans:longint;        s:array[1..6]of string;        p:array[1..6]of boolean;        t:array[0..46657]of string;procedure ss(l,r:longint);var        i,j:longint;        mid:string;begin        i:=l;        j:=r;        mid:=t[(i+j)div 2];        while i<j do        begin                while t[i]<mid do inc(i);                while t[j]>mid do dec(j);                if i<=j then                begin                        t[0]:=t[i];                        t[i]:=t[j];                        t[j]:=t[0];                        inc(i);                        dec(j);                end;        end;        if j>l then ss(l,j);        if i<r then ss(i,r);end;begin        assign(input,'match.in');reset(input);        assign(output,'match.out');rewrite(output);        for a:=1 to 6 do                readln(s[a]);        for a:=1 to 6 do        begin                p[a]:=true;                for i:=1 to 6 do                begin                        if p[i]=true then                                continue;                        if s[i][1]<>s[a][1] then continue;                        p[i]:=true;                        for j:=1 to 6 do                        begin                                if p[j]=true then                                        continue;                                if s[j][1]<>s[a][2] then                                        continue;                                p[j]:=true;                                for k:=1 to 6 do                                begin                                        if p[k]=true then                                                continue;                                        if s[k][1]<>s[a][3] then                                                continue;                                        p[k]:=true;                                        for i1:=1 to 6 do                                        begin                                                if p[i1]=true then                                                        continue;                                                if (s[i1][1]<>s[i][2])                                                or(s[i1][2]<>s[j][2])                                                or(s[i1][3]<>s[k][2]) then                                                        continue;                                                p[i1]:=true;                                                for j1:=1 to 6 do                                                begin                                                        if p[j1]=true then                                                                continue;                                                        if (s[j1][1]<>s[i][3])                                                        or(s[j1][2]<>s[j][3])                                                        or(s[j1][3]<>s[k][3]) then                                                        continue;                                                        inc(ans);                                                        t[ans]:=s[i]+s[j]+s[k];                                                end;                                                p[i1]:=false;                                        end;                                        p[k]:=false;                                end;                                p[j]:=false;                        end;                        p[i]:=false;                end;                p[a]:=false;        end;        if ans=0 then        begin                writeln(0);                halt;        end;        ss(1,ans);        writeln(t[1][1],t[1][2],t[1][3]);        writeln(t[1][4],t[1][5],t[1][6]);        writeln(t[1][7],t[1][8],t[1][9]);        close(input);        close(output);end.
 
0 0
原创粉丝点击