博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 1004 Anagrams by Stack (dfs+stack)
阅读量:7069 次
发布时间:2019-06-28

本文共 829 字,大约阅读时间需要 2 分钟。

用到的主要是回溯,递归时先将所有字符放入栈中,在回溯时判断字符是否出栈并记录路径。栈是用字符数组模拟的。

另外这题的输出有点扯,每一行的最后一个i或o后面是有空格的,不需要处理,PE一次。

code:

#include<cstdio>
#include<cstring>
using 
namespace std ;
char str[
50] ;
char bstr[
50], estr[
50], ans[
100] ;
int blen, temp, h, r ;
void dfs(
int bpos, 
int epos, 
int count){
    
if(epos==blen){
        
int i ;
        
for(i=
0; i<count; i++)
            printf(
"
%c 
", ans[i]) ;
        printf(
"
\n
") ;
    }
    
if(bpos<blen){
        str[r++] = bstr[bpos] ;
        ans[count] = 
'
i
' ;
        dfs(bpos+
1, epos, count+
1) ;
        r -- ;
    }
    
if(h!=r&&estr[epos]==str[r-
1]){
        
char a = str[r-
1] ;
        r -- ;
        ans[count] = 
'
o
' ;
        dfs(bpos, epos+
1, count+
1) ;
        str[r++] = a ;
    }
}
int main(){
    
while(~scanf(
"
%s%s
", bstr, estr)){
        blen = strlen(bstr) ;
        h = r = 
0 ;
        printf(
"
[\n
") ;
        dfs(
0
0
0) ;
        printf(
"
]\n
") ;
    }
    
return 
0 ;} 

转载于:https://www.cnblogs.com/xiaolongchase/archive/2012/03/13/2394497.html

你可能感兴趣的文章
像外行一样思考,像专家一样实践
查看>>
paper
查看>>
sql*loader的直接加载方式和传统加载方式的性能差异
查看>>
android ListView点击item返回后listview滚动位置
查看>>
DevExpress XtraTreeList TreeList复选框选择
查看>>
CF Polycarpus' Dice (数学)
查看>>
Eclipse用法与技巧——导入工程时报错(already exist in the workspace)
查看>>
python面向对象
查看>>
洛谷P1067 多项式输出
查看>>
考研随笔1
查看>>
机器学习算法学习---关联分析算法(二)
查看>>
selenium_webdriver(python)的第一个脚本
查看>>
第九周总结--助教
查看>>
meta标签
查看>>
关于样式获取的小细节--屏蔽单位及计算样式
查看>>
fetch方法
查看>>
vs2013 boost signals
查看>>
【cpp】new delete
查看>>
ruby 数据类型Symbol
查看>>
12月11日学习内容整理:ORM中的基于双下划线的跨表查询,反向查询,聚合和分组...
查看>>