• P1032 字串变换


    题目链接:https://www.luogu.org/problem/P1032

    思路:

    采用BFS

    我们遍历字符串a的每个字符,判断当前字符串i位置之后可不可以替换,如果可以替换,我们就把替换后的字符串 a'  放入队列。

    如果出现的我们想要的字符串,根据BFS的性质,那么就直接记录此时的步数。

     1 #include <math.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <string>
     7 #include <string.h>
     8 #include <vector>
     9 #include <map>
    10 #include <stack>
    11 #include <set>
    12 #include <queue>
    13 
    14 
    15 #define LL long long
    16 #define INF 0x3f3f3f3f
    17 #define ls nod<<1
    18 #define rs (nod<<1)+1
    19 const int maxn = 20;
    20 const double eps = 1e-9;
    21 
    22 struct Node {
    23     std::string str;
    24     int step;
    25 };
    26 
    27 int cnt,n;
    28 
    29 std::string a,b;
    30 std::string ori[maxn],trans[maxn];
    31 std::map<std::string,int> mp;
    32 
    33 std::string tra(std::string str,int i,int j) {
    34     std::string ans = "";
    35     if (i+ori[j].length() > str.length()) {  // 说明后面没有ori[j].length()的长度的字符
    36         return ans;
    37     }
    38     for (int k=0;k<ori[j].length();k++) {
    39         if (str[i+k] != ori[j][k]) {
    40             return ans;
    41         }
    42     }
    43     ans = str.substr(0,i);
    44     ans += trans[j];
    45     ans += str.substr(i+ori[j].length());
    46     return ans;
    47 }
    48 
    49 void bfs() {
    50     std::queue<Node> q;
    51     Node st;
    52     st.step = 0;
    53     st.str = a;
    54     q.push(st);
    55     while (!q.empty()) {
    56         Node ed = q.front();
    57         q.pop();
    58         std::string temp;
    59         if (mp.count(ed.str) == 1) {
    60             continue;
    61         }
    62         if (ed.str == b) {
    63             cnt = ed.step;
    64             break;
    65         }
    66         mp[ed.str] = 1;
    67         for (int i=0;i<ed.str.length();i++) {
    68             for (int j=0;j<n;j++) {
    69                 temp = tra(ed.str,i,j);
    70                 if (temp != "") {
    71                     Node u;
    72                     u.str = temp;
    73                     u.step = ed.step + 1;
    74                     q.push(u);
    75                 }
    76             }
    77         }
    78     }
    79     if (cnt > 10 || cnt == 0) {
    80         printf("NO ANSWER!
    ");
    81     }
    82     else
    83         printf("%d
    ",cnt);
    84 }
    85 
    86 int main() {
    87     //freopen("../in.txt","r",stdin);
    88     std::cin >> a >> b;
    89     while (std::cin >> ori[n] >> trans[n])
    90         n++;
    91     bfs();
    92     return 0;
    93 }
  • 相关阅读:
    我渴望自由和成功(愿与君共勉)
    gdb使用 和core dump文件调试
    谷歌浏览器快捷键大全
    Android适屏
    BestCoder Round #33
    NAT&amp;Port Forwarding&amp;Port Triggering
    程序员应该学习的书
    java代码调用rtx发送提醒消息
    js实现excel导出
    一个跨界程序员:不务正业的这几年,我如何让自己的收入翻了十倍(转)
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11861941.html
Copyright © 2020-2023  润新知