acm 试题 字符串排序
2007-05-12 18:49
363 查看
Given a string of letters(A-Z),your task is to arrange them in alphabetic order.
Following is an example:
A string "BAC" contains 3 letters B,A and C,you should output
ABC
ACB
BAC
BCA
CAB
CBA
In the output file.
A string may contain several letters same,for example "BBC" you should output like this:
BBC
BCB
CBB
Input
The first line of input contains a single integer t, the number of test cases,followed by the input data for each test data.Each test case is a string of n(1<=n<=26) letters.
Output
You should output Case K: in the first line and the sequences of arrangement in the following lines of each case.
Sample Input
2
BAC
BBC
Sample Output
Case 1:
ABC
ACB
BAC
BCA
CAB
CBA
Case 2:
BBC
BCB
CBB
这道题目,我采用的是递归算法实现的。 采用的链串 结构;
算法简单描述为:对一个链串,有个标记开始置换的指针,然后向后移动,直到末尾,然后置换的指针前移,重复上面的过程。如果标记的指针的内容和移动指针内容不同就打印出来;
源代码如下:
#include <stdio.h>
#include <iostream>
using namespace std ;
typedef struct str
{
char ch ;
struct str * next ;
}str ;
void print(str * s); //打印
void create(str *&s);//建立
void travel(str *&s,str * r) ;//遍历,采用递归算法,实现排列问题
void swap(char &a , char & b) ;//交换
int main(int argc, char* argv[])
{
str * s ;
create(s);
print(s);
travel(s,s->next);
system("PAUSE");
return 0;
}
void create(str *&s)//建立链串
{
s = new str ;
str * p ;
str * q = s ;
char ch ;
while((ch = getchar())!= '/n')
{
p = new str ;
p->ch = ch ;
p->next = NULL ;
q->next = p ;
q = p ;
}
q->next = NULL ;
}
void print(str * s)
{
str * p ;
p = s->next ;
while(p != NULL)
{
cout<<p->ch ;
p = p->next ;
}
putchar('/n');
}
void swap(char &a , char & b)
{
char temp ;
temp = a ;
a = b ;
b = temp ;
}
void travel(str *&s,str * r) //遍历
{
str * p = r;
str * q = p->next ;
if(r->next == NULL)
{
return ;
}
else
{
while(p!= NULL)
{
q = p->next ;
while(q != NULL)
{
swap(p->ch,q->ch);
if(p->ch != q->ch)
{
print(s);
travel(s,p->next);
}
swap(p->ch,q->ch);
q= q->next ;
}
p = p->next ;
}
}
}
Following is an example:
A string "BAC" contains 3 letters B,A and C,you should output
ABC
ACB
BAC
BCA
CAB
CBA
In the output file.
A string may contain several letters same,for example "BBC" you should output like this:
BBC
BCB
CBB
Input
The first line of input contains a single integer t, the number of test cases,followed by the input data for each test data.Each test case is a string of n(1<=n<=26) letters.
Output
You should output Case K: in the first line and the sequences of arrangement in the following lines of each case.
Sample Input
2
BAC
BBC
Sample Output
Case 1:
ABC
ACB
BAC
BCA
CAB
CBA
Case 2:
BBC
BCB
CBB
这道题目,我采用的是递归算法实现的。 采用的链串 结构;
算法简单描述为:对一个链串,有个标记开始置换的指针,然后向后移动,直到末尾,然后置换的指针前移,重复上面的过程。如果标记的指针的内容和移动指针内容不同就打印出来;
源代码如下:
#include <stdio.h>
#include <iostream>
using namespace std ;
typedef struct str
{
char ch ;
struct str * next ;
}str ;
void print(str * s); //打印
void create(str *&s);//建立
void travel(str *&s,str * r) ;//遍历,采用递归算法,实现排列问题
void swap(char &a , char & b) ;//交换
int main(int argc, char* argv[])
{
str * s ;
create(s);
print(s);
travel(s,s->next);
system("PAUSE");
return 0;
}
void create(str *&s)//建立链串
{
s = new str ;
str * p ;
str * q = s ;
char ch ;
while((ch = getchar())!= '/n')
{
p = new str ;
p->ch = ch ;
p->next = NULL ;
q->next = p ;
q = p ;
}
q->next = NULL ;
}
void print(str * s)
{
str * p ;
p = s->next ;
while(p != NULL)
{
cout<<p->ch ;
p = p->next ;
}
putchar('/n');
}
void swap(char &a , char & b)
{
char temp ;
temp = a ;
a = b ;
b = temp ;
}
void travel(str *&s,str * r) //遍历
{
str * p = r;
str * q = p->next ;
if(r->next == NULL)
{
return ;
}
else
{
while(p!= NULL)
{
q = p->next ;
while(q != NULL)
{
swap(p->ch,q->ch);
if(p->ch != q->ch)
{
print(s);
travel(s,p->next);
}
swap(p->ch,q->ch);
q= q->next ;
}
p = p->next ;
}
}
}
相关文章推荐
- 字符串排序的程序代码 acm试题
- acm--字符串排序
- 【ACM】1043:字符串排序
- ACMSTEP 1.3.5 排序//排序 字符串
- 九度OJ 1066 清华01机试题之字符串排序
- 2016年华为秋招机试题——2.字符串排序(200分)
- 字符串排序--acm.njupt
- acm--等长字符串排序(头都大了...)
- 华为OJ平台试题 ——字符串:字符串比较排序
- 【ACM】1043:字符串排序
- 新疆大学OJ(ACM) 1047: string 字符串排序
- ACM 字符串排序
- acm--字符串排序(指针和引用)
- ACM 算法竞赛 八大基础算法 模拟法 字符串 递归分治 排序 枚举 贪心 递推
- 2015年华为校招机试题和代码实现(分解字符串,拼音转数字,去除重复字符并排序,等式变换)
- 华为机试题: 整型字符串排序
- acm--不等长字符串排序(指向指针的指针)
- ACM--字母排序--HDOJ 1379--DNA Sorting--字符串
- 按ASC码对字符串排序
- java中JSON对象字符串排序问题