zoukankan      html  css  js  c++  java
  • ZOJ1004 && HDU1515 dfs回溯

    题目大意:

    就是通过一个栈进行字母入栈出栈得到想要的字符,把所有可能的方式全部输出

    自己写的方法一开始一直不能过,后来参考了别人的方法,写出来的比较简单的代码

    这段代码更有回溯的感觉,自己后来又把自己原来想法的代码写了一遍,终于写出来了,不过有点让人头晕

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <stack>
     4 using namespace std;
     5 stack<char> s;
     6 char str1[10005],str2[10005],io[10005];
     7 int n,m;
     8 void dfs(int x,int y,int cnt)
     9 {
    10     if(y==m){
    11        // printf("%c ",io[0]);
    12         for(int i=0;i<cnt;i++)
    13             printf("%c ",io[i]);
    14         puts("");
    15         return;
    16     }
    17 
    18     if(x<n){
    19         io[cnt]='i';
    20         s.push(str1[x]);
    21         dfs(x+1,y,cnt+1);
    22         s.pop();
    23     }
    24 
    25     if(!s.empty() && s.top() == str2[y]){
    26         io[cnt]='o';
    27         char a = s.top();
    28         s.pop();
    29         dfs(x,y+1,cnt+1);
    30         s.push(a);
    31     }
    32 }
    33 int main()
    34 {
    35     while(~scanf("%s%s",str1,str2)){
    36         puts("[");
    37 
    38         n=strlen(str1);
    39         m=strlen(str2);
    40         //printf("%d  %d
    ",n,m);
    41         while(!s.empty())
    42             s.pop();
    43 
    44         dfs(0,0,0);
    45 
    46         puts("]");
    47     }
    48     return 0;
    49 }


    不断传入出栈的值和对应的str2上的字符进行比较,只有一只匹配正确才继续,若匹配成功最后一个字符,就输出io[]保存的过程字符

    k表示保存了k个过程符,x表示str1入栈了x个字符,y表示比较到了str2的第y个字符

    自己写的过程中因为想不到上面那么好的思路,就有点乱,防止数组越界,只能不断加各种限制,自己的思路想想应该还是只有自己看得懂吧~~

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <stack>
     4 using namespace std;
     5 
     6 stack<char> s;
     7 char str1[10005] , str2[10005] , io[10005];
     8 int n,m;
     9 
    10 void dfs(char a,int k,int x,int y)
    11 {
    12     if(y>0){
    13         if(a != str2[y-1])
    14             return;
    15     
    16         else if(y == m){
    17             for(int i=0;i<k;i++)
    18                 printf("%c ",io[i]);
    19             puts("");
    20             return;
    21         }
    22     }
    23     
    24     if(y>=m)
    25         return;
    26     
    27     if(s.empty()){
    28         if(x>=n)
    29             return;
    30         io[k] = 'i';
    31         s.push(str1[x]);
    32         dfs(a,k+1,x+1,y);
    33         s.pop();
    34     }
    35     else{
    36         if(x<n){
    37             io[k] = 'i';
    38             s.push(str1[x]);
    39             dfs(a,k+1,x+1,y);
    40             s.pop();
    41         }
    42         
    43         io[k]='o';
    44         char b = s.top();
    45         s.pop();
    46         dfs(b,k+1,x,y+1);
    47         s.push(b);
    48     }
    49 }
    50 
    51 int main()
    52 {
    53     while(~scanf("%s%s",str1,str2)){
    54         puts("[");
    55 
    56         n=strlen(str1);
    57         m=strlen(str2);
    58         //printf("%d  %d
    ",n,m);
    59         while(!s.empty())
    60             s.pop();
    61         
    62         dfs('a',0,0,0);
    63 
    64         puts("]");
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    用R语言完成的交通可视化报告
    二维码(带有图片)的生成
    大文件数据去重复
    shell 脚本大文件处理
    Domain-specific language 领域特定语言
    cacheed 限制 4节点 3000万 es 批量删除 shell脚本练习 elasticsearch_action
    cached
    广告中嵌入非广告 非广告中嵌入广告
    js 四种调用方式 Method Invocation Pattern
    js 变量作用域
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/3993930.html
Copyright © 2011-2022 走看看