NOIP 2002字串变换 解题报告(双向宽搜)

2 篇文章 0 订阅
1 篇文章 0 订阅

在线评测:

http://codevs.cn/problem/1099/


整体思路:

双向宽搜,,


失误之处:

1、对于一个坚持从1开始循环的人,,字符串的0开始多么的不友好,,,

2、字符串处理蒙蔽

3、对于自己定义的一些变量,没有下精准的定义,导致写着写着自己都迷糊了,,,

4、一个用于比较两个字符串是否相等的函数,若相等则返回用多少次变成这个串,否则返回0,但是没有考虑初始相等,返回0误判的情况。

5、

当你自己瞎搞了一个类型,并想用set来进行判重时,小于号不能瞎重载,要满足以下条件

A < B那么A,B,值不变化时,A恒<B

A < B, B<C 那么 A<C

如果A<B,B一定不小于A(如果a 不小于 b,b也不小于a,那么a,b会被认为是相等的。。(如果瞎定义< 可能不相等的也被搞成相等了。。))


体会心得:

1、适当的熟练应用从0开始和从1开始,能进能退,,

2、变量要精准,,不要稀里糊涂,,

3、字符串什么的要多练一练,

4、多注意stl的细节,,多考虑特殊情况。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using  namespace  std;
struct  zh
{
     char  a[150],b[150];
};
struct  zz
{
     char  a[150];
     int  cs;
};
queue <zz> dl1;
queue <zz> dl2;
queue <zz> tpd;
bool  js;
zh sz[10];
int  max1,max2;
char  A[150],B[150],tp1[150],tp2[150];
int  sum;
void  sread()
{
     scanf ( "%s%s" ,A,B);
     while  ( scanf ( "%s%s" ,tp1,tp2) > 0)
     {
         sum++;
         strcpy (sz[sum].a,tp1);
         strcpy (sz[sum].b,tp2);
     }
}
int  check1(zz x,zz y)
{
     int  lenx =  strlen (x.a),leny =  strlen (y.a);
     if  (lenx != leny)  return  0;
     for  ( int  i = 0;i < lenx;i++)
     {
         if  (x.a[i] != y.a[i])  return  0;
     }
     return  1;
}
int  check2(zz x,zz y)
{
     int  lenx =  strlen (x.a),leny =  strlen (y.a);
     if  (lenx != leny)  return  0;
     for  ( int  i = 0;i < lenx;i++)
     {
         if  (x.a[i] != y.a[i])  return  0;
     }
     return  1;
}
void  zf( int  x,zz ts, int  cs)
{
     zh tp = sz[x];
     int  lenn =  strlen (tp.a);
     int  len1 =  strlen (tp.b);
     int  qd =  strlen (ts.a);
     for  ( int  i = 0;i < qd;i++)
     {
         bool  zpd =  true ;
         for  ( int  j = 0;j <lenn;j++)
         {
             if  (ts.a[i + j] != tp.a[j])
             {
                 zpd =  false ;
                 break ;
             }
         }
         
         if  (zpd)
         {
             zz ztp;
             for  ( int  j = 0;j < i ;j++)
             {
                 ztp.a[j] = ts.a[j];
             }
             for  ( int  j = i;j < i + len1;j++)
             {
                 ztp.a[j] = tp.b[j - i];
             }
             for  ( int  j = i + len1;j <= qd - lenn + len1;j++)
             {
                 ztp.a[j] = ts.a[j - (len1 - lenn)];
             }
             
             ztp.cs = cs;
             dl1.push(ztp);
             while  (!dl2.empty())
             {
                 zz wtp = dl2.front();
                 dl2.pop();
                 tpd.push(wtp);
                 if  (check1(wtp,ztp))
                 {
                     int  itp = cs + wtp.cs;
                     if  (itp > 10)  printf ( "NO ANSWER!\n" ); else
                     printf ( "%d\n" ,itp);
                     js =  true ;
                     return ;
                 }
             }
             while  (!tpd.empty())
             {
                 zz wtp = tpd.front();
                 tpd.pop();
                 dl2.push(wtp);
             }
         }
     }
}
 
void  ff( int  x,zz ts, int  cs)
{
     zh tp = sz[x];
     int  lenn =  strlen (tp.b);
     int  len1 =  strlen (tp.a);
     int  qd =  strlen (ts.a);
     for  ( int  i = 0;i < qd;i++)
     {
         bool  zpd =  true ;
         for  ( int  j = 0;j <lenn;j++)
         {
             if  (ts.a[i + j] != tp.b[j])
             {
                 zpd =  false ;
                 break ;
             }
         }
         
         if  (zpd)
         {
             zz ztp;
             for  ( int  j = 0;j < i ;j++)
             {
                 ztp.a[j] = ts.a[j];
             }
             for  ( int  j = i;j < i + len1;j++)
             {
                 ztp.a[j] = tp.a[j - i];
             }
             for  ( int  j = i + len1;j <=qd - lenn + len1 ;j++)
             {
                 ztp.a[j] = ts.a[j - (len1 - lenn)];
             }
             ztp.cs = cs;
             dl2.push(ztp);
             while  (!dl1.empty())
             {
                 zz wtp = dl1.front();
                 dl1.pop();
                 tpd.push(wtp);
                 if  (check1(wtp,ztp))
                 {
                     int  itp = cs + wtp.cs;
                     if  (itp > 10)  printf ( "NO ANSWER!\n" ); else
                     printf ( "%d\n" ,itp);
                     js =  true ;
                     return ;
                 }
             }
             while  (!tpd.empty())
             {
                 zz wtp = tpd.front();
                 tpd.pop();
                 dl1.push(wtp);
             }
         }
     }
}
void  swork()
{
     while  (1)
     {
         if  (max1 + max2 >= 12)
         {
             printf ( "NO ANSWER!\n" );
             return ;
         }
         if  (dl1.size() <= dl2.size())
         {
             zz tp = dl1.front();
             dl1.pop();
             int  tcs = tp.cs;
             max1 = tcs;
             for  ( int  i = 1;i <= sum;i++)
             {
                 zf(i,tp,tcs + 1);
                 if  (js)  return ;
             }  
         } else
         {
             zz tp = dl2.front();
             dl2.pop();
             int  tcs = tp.cs;
             max2 = tcs;
             for  ( int  i = 1;i <= sum;i++)
             {
                 ff(i,tp,tcs + 1);
                 if  (js)  return ;
             }
         }
         
     }
}
void  sinit()
{
     zz stp;
     strcpy (stp.a,A);
     stp.cs = 0;
     dl1.push(stp);
     strcpy (stp.a,B);
     dl2.push(stp);
}
int  main()
{
     sread();
     sinit();
     swork();
     retur

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值