還是勇敢的dfs下去
不過因為要照字典序 然後把全排列弄出來之後再sort會很慢
所以就直接先對原本的字串sort然後照順序拿就好了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<bits/stdc++.h> using namespace std; string s; bool took[8]; vector<string> ans; void t(string now){ if(now.length()==s.length()){ ans.push_back(now); return; } for(int i=0;i<s.length();i++){ if(!took[i]){ took[i]=1; t(now+s[i]); took[i]=0; } } return; } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); while(cin>>s){ sort(s.begin(),s.end()); memset(took,0,sizeof(took)); ans.clear(); t(""); for(auto i:ans)cout<<i<<endl; cout<<endl; } } |
沒有留言:
張貼留言