Skip to content

Commit 8606c9b

Browse files
committed
Next permutation
1 parent bb1a649 commit 8606c9b

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

palindrome-d.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
string s;
6+
cin >> s;
7+
8+
int l = s.size() / 2;
9+
sort(s.begin(), s.begin() + l);
10+
// cout << s << endl;
11+
12+
do {
13+
14+
copy(s.begin(), s.begin() + l, ostream_iterator<char>(cout, ""));
15+
if (s.size() & 1) cout << s[l];
16+
// copy(s.rbegin()-l, s.rend(), ostream_iterator<char>(cout, ""));
17+
//doesn't work
18+
19+
for(int i = l - 1; i >=0; --i) cout << s[i];
20+
21+
cout << "\n";
22+
23+
} while (next_permutation(s.begin(), s.begin() + l));
24+
}

palindrome-h.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
char s[100010],half[100010];
6+
cin>>s;
7+
int i,len=strlen(s);
8+
for(i=0;i<len/2;i++){
9+
half[i]=s[i];
10+
}
11+
sort(half,half+len/2);
12+
if(len%2==0){
13+
do{
14+
cout<<half;
15+
for(int i=len/2-1;i>=0;i--){
16+
cout<<half[i];
17+
}
18+
cout<<"\n";
19+
}while(next_permutation(half,half+len/2));
20+
21+
}
22+
else{
23+
do{
24+
cout<<half;
25+
cout<<s[len/2];
26+
for(int i=len/2-1;i>=0;i--){
27+
cout<<half[i];
28+
}
29+
cout<<"\n";
30+
}while(next_permutation(half,half+len/2));
31+
32+
}
33+
return 0;
34+
}

0 commit comments

Comments
 (0)