日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ 1004 Anagrams by Stack(DFS+数据结构)

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ 1004 Anagrams by Stack(DFS+数据结构) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Anagrams by Stack

題目鏈接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4

題目大意:輸入兩個字符串序列,判斷能否通過對第一個字符串進行進棧出棧操作得到第二個字符串,若能則輸出所有能達到的進出棧操作過程。我通過全排列每得到一組操作過程,則用函數按照這個操作過程,判斷能否得到第二個字符串,若能則表明此操作過程可行,輸出。

代碼如下:

?

1 # include<iostream> 2 # include<stack> 3 # include<vector> 4 # include<algorithm> 5 using namespace std; 6 7 string str1,str2; 8 stack<char> p; 9 vector<char> ans; 10 int len; 11 12 void print(){ 13 for(int i =0; i<ans.size(); i++) 14 cout<<ans[i]<<" "; //注意空格 15 cout<<endl; 16 } 17 18 void dfs(int x,int y){ 19 if(x==len && y==len) 20 print(); 21 if(x+1<=len){ 22 p.push(str1[x]); 23 ans.push_back('i'); 24 dfs( x+1 , y); 25 p.pop(); 26 ans.pop_back(); 27 } 28 if(y+1<=x && y+1<=len &&p.top() == str2[y]){ 29 char temp = p.top(); 30 p.pop(); 31 ans.push_back('o'); 32 dfs(x,y+1); 33 p.push(temp); 34 ans.pop_back(); 35 } 36 } 37 38 int main(){ 39 while(cin >> str1 >> str2){ 40 len =str1.length(); 41 string t1 = str1, t2 = str2; 42 sort(t1.begin(),t1.end()); 43 sort(t2.begin(),t2.end()); 44 cout<<"["<<endl; 45 if(t1 == t2){ 46 dfs(0,0); 47 } 48 cout<<"]"<<endl; 49 } 50 return 0; 51 }

?

?

?

?

轉載于:https://www.cnblogs.com/acm-bingzi/p/3258815.html

總結

以上是生活随笔為你收集整理的ZOJ 1004 Anagrams by Stack(DFS+数据结构)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。