LOI 54 成立一周年纪(zuo)念(si)

---恢复内容开始---

本来今天双向BFS题解都写了一半了,忘保存,然后关了,所以就。。。。。。呵呵

然后,今天是LOI 54成立一周年(或许吧,时间不是太精确)。

so,今天不写题解了,作死啊。。。。。。。。

今天有一道题还没A,但是快了,毕竟,string-findnext还不会。

算了,不说了,贴个代码草草收场吧。

题目:

1099 字串变换

2002年NOIP全国联赛提高组

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
 
 
 
题目描述 Description

已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则):
     A1$ -> B1$
     A2$ -> B2$
  规则的含义为:在 A$中的子串 A1$ 可以变换为 B1$、A2$ 可以变换为 B2$ …。
    例如:A$='abcd' B$='xyz'
  变换规则为:
    ‘abc’->‘xu’ ‘ud’->‘y’ ‘y’->‘yz’

  则此时,A$ 可以经过一系列的变换变为 B$,其变换的过程为:
   ‘abcd’->‘xud’->‘xy’->‘xyz’

  共进行了三次变换,使得 A$ 变换为B$。

输入描述 Input Description

输入格式如下:

   A$ B$
   A1$ B1$ \
   A2$ B2$  |-> 变换规则
   ... ... / 
  所有字符串长度的上限为 20。

输出描述 Output Description

若在 10 步(包含 10步)以内能将 A$ 变换为 B$ ,则输出最少的变换步数;否则输出"NO ANSWER!"

样例输入 Sample Input

abcd xyz
abc xu
ud y
y yz

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

hehe 

 

80分代码(有个bug以后再说):

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 struct state
 7 {
 8     string a;
 9     int dep;
10 }fir[5010],las[5010];
11 int cnt=1;
12 string s[8],t[8];
13 int lens[8],lent[8];
14 void bfs()
15 {
16     int head1,tail1,head2,tail2;
17     head1=tail1=head2=tail2=1;
18     while(head1<=tail1 && head2<=tail2)
19     {
20         if (fir[head1].dep+las[head2].dep>10)
21         {
22             printf("NO ANSWER!\n");
23             return ;
24         }
25         for(int i=1;i<=cnt;i++)
26         {
27             int pos=fir[head1].a.find(s[i]);
28             if(pos!=string::npos)
29             {
30                 string ss="";
31                 for(int j=0;j<pos;j++)ss+=fir[head1].a[j];
32                 ss+=t[i];
33                 for(int j=pos+lens[i];j<fir[head1].a.length();j++)ss+=fir[head1].a[j];
34                 tail1++;
35                 fir[tail1].a=ss;
36                 fir[tail1].dep=fir[head1].dep+1;
37           //      cout << fir[tail1].a << endl;
38                 for (int k=1;k<=tail2;k++)
39                     if (fir[tail1].a==las[k].a)
40                     {
41                        printf("%d\n",fir[tail1].dep+las[k].dep);
42                        return;
43                     }
44             }
45         }
46         head1++;
47         for(int i=1;i<=cnt;i++)
48         {
49             int pos=las[head2].a.find(t[i]);
50             if(pos!=string::npos)
51             {
52                 string ss="";
53                 for(int j=0;j<pos;j++)ss+=las[head2].a[j];
54                 ss+=s[i];
55                 for(int j=pos+lent[i];j<las[head2].a.length();j++)ss+=las[head2].a[j];
56                 tail2++;
57                 las[tail2].a=ss;
58                 las[tail2].dep=las[head2].dep+1;
59             //    cout << las[tail2].a << endl;
60                 for (int k=1;k<=tail1;k++)
61                     if (fir[k].a==las[tail2].a)
62                     {
63                        printf("%d\n",fir[k].dep+las[tail2].dep);
64                        return;
65                     }
66             }
67         }
68         head2++;
69     }
70     printf("NO ANSWER!\n");
71 }
72 int main()
73 {
74     cin>>fir[1].a>>las[1].a;
75     fir[1].dep=las[1].dep=0;
76     while(cin>>s[cnt]>>t[cnt])
77     {
78         lens[cnt]=s[cnt].length();
79         lent[cnt]=t[cnt].length();
80         cnt++;
81     }
82     cnt--;
83     bfs();
84 }
View Code

 

posted @ 2014-10-23 16:41  Skyvot  阅读(244)  评论(0编辑  收藏  举报