字典序问题
来源:互联网 发布:软件可靠性英文 编辑:程序博客网 时间:2024/06/05 11:44
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
class permu {
private:
int sort[99999];
int size;
public:
permu() {
memset(sort, 0, sizeof(sort));
size = 0;
};
permu(int n) {
memset(sort, 0, sizeof(sort));
for (int i = 0; i < n; i++) {
sort[i] = i;
}
size = n;
}
permu(const permu& other) {
memset(sort, 0, sizeof(sort));
for (int i = 0; i < other.size; i++) {
sort[i] = other.sort[i];
}
size = other.size;
}
void turn (int n, int m) {
int temp = sort[n];
sort[n] = sort[m];
sort[m] = temp;
}
void preper() {
if (size == 2) {
turn(0,1);
return;
}
bool isfin = 1;
int i, j;
for (i = 0; i < size - 1; i++) { // Judge if finished
if (sort[i] > sort[i + 1]) {
isfin = 0;
break;
}
}
if (!isfin) {
for (i = size - 1; i >= 0; i--) {
if (sort[i] < sort[i-1]) {
i--;
break;
}
}
for (j = i; j <= size-1; j++) {
if (sort[j] > sort[i]) {
j--;
break;
}
if (j == size-1) {
break;
}
}
turn(i, j);
reverse(i+1, size - 1);
} else {
reverse(0, size-1);
}
};
inline permu operator++(int n) {
permu temp = *this;
nextper();
return temp;
};
inline permu& operator++() {
nextper();
return *this;
};
inline permu operator--(int n) {
permu temp = *this;
preper();
return temp;
};
inline permu& operator--() {
preper();
return *this;
};
permu& operator =(permu a) {
size = a.size;
for (int i = 0; i < size; ++i) {
sort[i] = a.sort[i];
}
return *this;
};
void reverse(int m, int n) {
if (m == n) return;
int a[100];
int i, j;
for (i = n, j = 0; i >= m; i--, j++) {
a[j] = sort[i];
}
for (i = m, j = 0; i <= n; i++, j++) {
sort[i] = a[j];
}
};
void nextper() { //++
if (size == 2) {
turn(0,1);
return;
}
bool isfin = 1;
int i, j;
for (i = 0; i < size - 1; i++) { // Judge if finished
if (sort[i] < sort[i + 1]) {
isfin = 0;
break;
}
}
if (!isfin) {
for (j = size - 1; j > 0; j--) {
if (sort[j] > sort[j - 1]) {
j--;
break;
}
}
for (i = j; i < size - 1; i++) {
if (sort[i] < sort[j]) {
i--;
// the position to be turned
break;
}
}
turn(i, j);
reverse(j+1, size - 1);
} else {
for (int t = 0; t < size; t++) {
sort[t] = t;
}
reverse(0, size-1);
}
};
friend ostream& operator<<(ostream& out, permu & a);
};
ostream& operator<<(ostream& out, permu &a) {
for (int i = 0; i < a.size; i++) {
out << a.sort[i] << " ";
}
return out;
}
#include <iostream>
#include <cstring>
using namespace std;
class permu {
private:
int sort[99999];
int size;
public:
permu() {
memset(sort, 0, sizeof(sort));
size = 0;
};
permu(int n) {
memset(sort, 0, sizeof(sort));
for (int i = 0; i < n; i++) {
sort[i] = i;
}
size = n;
}
permu(const permu& other) {
memset(sort, 0, sizeof(sort));
for (int i = 0; i < other.size; i++) {
sort[i] = other.sort[i];
}
size = other.size;
}
void turn (int n, int m) {
int temp = sort[n];
sort[n] = sort[m];
sort[m] = temp;
}
void preper() {
if (size == 2) {
turn(0,1);
return;
}
bool isfin = 1;
int i, j;
for (i = 0; i < size - 1; i++) { // Judge if finished
if (sort[i] > sort[i + 1]) {
isfin = 0;
break;
}
}
if (!isfin) {
for (i = size - 1; i >= 0; i--) {
if (sort[i] < sort[i-1]) {
i--;
break;
}
}
for (j = i; j <= size-1; j++) {
if (sort[j] > sort[i]) {
j--;
break;
}
if (j == size-1) {
break;
}
}
turn(i, j);
reverse(i+1, size - 1);
} else {
reverse(0, size-1);
}
};
inline permu operator++(int n) {
permu temp = *this;
nextper();
return temp;
};
inline permu& operator++() {
nextper();
return *this;
};
inline permu operator--(int n) {
permu temp = *this;
preper();
return temp;
};
inline permu& operator--() {
preper();
return *this;
};
permu& operator =(permu a) {
size = a.size;
for (int i = 0; i < size; ++i) {
sort[i] = a.sort[i];
}
return *this;
};
void reverse(int m, int n) {
if (m == n) return;
int a[100];
int i, j;
for (i = n, j = 0; i >= m; i--, j++) {
a[j] = sort[i];
}
for (i = m, j = 0; i <= n; i++, j++) {
sort[i] = a[j];
}
};
void nextper() { //++
if (size == 2) {
turn(0,1);
return;
}
bool isfin = 1;
int i, j;
for (i = 0; i < size - 1; i++) { // Judge if finished
if (sort[i] < sort[i + 1]) {
isfin = 0;
break;
}
}
if (!isfin) {
for (j = size - 1; j > 0; j--) {
if (sort[j] > sort[j - 1]) {
j--;
break;
}
}
for (i = j; i < size - 1; i++) {
if (sort[i] < sort[j]) {
i--;
// the position to be turned
break;
}
}
turn(i, j);
reverse(j+1, size - 1);
} else {
for (int t = 0; t < size; t++) {
sort[t] = t;
}
reverse(0, size-1);
}
};
friend ostream& operator<<(ostream& out, permu & a);
};
ostream& operator<<(ostream& out, permu &a) {
for (int i = 0; i < a.size; i++) {
out << a.sort[i] << " ";
}
return out;
}
0 0
- 字典序问题
- 字典序问题
- 字典序问题
- 算法----字典序问题
- 字典序问题
- 字典序问题
- 字典序问题
- 字典序问题
- 字典序问题
- 字典序问题
- 字典序问题
- 递归 :字典序问题
- 字典序问题
- 字典序最小问题
- 字典序问题
- 字典序问题
- 字典序问题
- 字典序问题
- Redis系列-php怎么通过redis扩展使用redis
- Android保存Bitmap到文件
- localStorage的跨域问题解决方案
- android studio 2.2 C++ 支持(CMAKE方法)
- Kafka+Spark Streaming+Redis实时计算整合实践
- 字典序问题
- 在PPT中播放插入flash的指定片段
- java多线程---等待/唤醒以及生产者消费者经典同步Lock的实现
- eclipse Maven配置
- linux基本命令(47)——iostat命令
- Flask Web 开发学习稿(二)
- 给图片设置填充父控件,但是图片显示出来宽高还是有空白
- Android获得小图
- 【JZOJ 3427】归途与征程