P1032 字串變換

2022-04-17 01:41:10 字數 1235 閱讀 6454

思路:採用bfs

我們遍歷字串a的每個字元,判斷當前字串i位置之後可不可以替換,如果可以替換,我們就把替換後的字串 a'  放入佇列。

如果出現的我們想要的字串,根據bfs的性質,那麼就直接記錄此時的步數。

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 

7 #include

8 #include 9 #include 10 #include 11 #include

12 #include 13

1415

#define ll long long

16#define inf 0x3f3f3f3f

17#define ls nod<<1

18#define rs (nod<<1)+1

19const

int maxn = 20;20

const

double eps = 1e-9;21

22struct

node ;

2627

intcnt,n;

2829 std::string

a,b;

30 std::string

ori[maxn],trans[maxn];

31 std::mapstring,int>mp;

3233 std::string tra(std::string str,int i,int

j) 38

for (int k=0;k) 42}

43 ans = str.substr(0

,i);

44 ans +=trans[j];

45 ans += str.substr(i+ori[j].length());

46return

ans;47}

4849

void

bfs()

62if (ed.str ==b)

66 mp[ed.str] = 1;67

for (int i=0;i) 76}

77}78}

79if (cnt > 10 || cnt == 0

) 82

else

83 printf("

%d\n

",cnt);84}

8586

intmain()

P1032 字串變換

迭代加深難題 右手進入傳送門 大意是這樣的 給定兩個字串a,b以及至多六個變換規則 規則指a1 b1,a2 b2,在a中的子串 a1可以變換為b1,a2可以變換為 b2 求最少變換步數,若在10步 包含10步 以內能將a變換為b,則輸出最少的變換步數 否則輸出 no answer 看到題目要求的10...

P1032 字串變換

已知有兩個字串 a,b 及一組字串變換的規則 至多6個規則 a1 b1 a2 b2 規則的含義為 在 a 中的子串 a1 可以變換為 b1 a2 可以變換為 b2 例如 a abcd b xyz 變換規則為 abc xu ud y y yz 則此時,a 可以經過一系列的變換變為 b,其變換的過程為 ...

P1032 字串變換 字串

已知有兩個字串 a,b 及一組字串變換的規則 至多6個規則 a1 b1 a2 b2 規則的含義為 在 a 中的子串 a1 可以變換為 b1 a2 可以變換為 b2 例如 a abcd b xyz 變換規則為 abc xu ud y y yz 則此時,a 可以經過一系列的變換變為 b,其變換的過程為 ...