简单题


View Code
#include
<
iostream
>
#include
<
cstdio
>
#include
<
cstdlib
>
#include
<
cstring
>
#include
<
algorithm
>
using
namespace
std;
char
st[
20
];
int
num[
20
];
int
n;
int
len;
char
ans[
20
];
bool
cmp(
const
char
&
a,
const
char
&
b)
{
int
x
=
a
–
‘
A
‘
;
int
y
=
b
–
‘
A
‘
;
if
(x
>=
26
)
x
-=
‘
a
‘
–
‘
A
‘
;
if
(y
>=
26
)
y
-=
‘
a
‘
–
‘
A
‘
;
if
(x
!=
y)
return
x
<
y;
return
a
<
b;
}
void
dfs(
int
a)
{
if
(a
==
len)
{
ans[a]
=
‘
\0
‘
;
printf(
“
%s\n
“
, ans);
return
;
}
for
(
int
i
=
0
; i
<
n; i
++
)
if
(num[i]
>
0
)
{
ans[a]
=
st[i];
num[i]
—
;
dfs(a
+
1
);
num[i]
++
;
}
}
int
main()
{
//
freopen(“t.txt”, “r”, stdin);
int
t;
scanf(
“
%d
“
,
&
t);
while
(t
—
)
{
scanf(
“
%s
“
, st);
sort(st, st
+
strlen(st), cmp);
memset(num,
0
,
sizeof
(num));
int
j
=
0
;
len
=
strlen(st);
for
(
int
i
=
0
; i
<
len; i
++
)
if
(st[i]
==
st[j])
num[j]
++
;
else
{
st[
++
j]
=
st[i];
num[j]
=
1
;
}
n
=
j
+
1
;
dfs(
0
);
}
return
0
;
}
<
iostream
>
#include
<
cstdio
>
#include
<
cstdlib
>
#include
<
cstring
>
#include
<
algorithm
>
using
namespace
std;
char
st[
20
];
int
num[
20
];
int
n;
int
len;
char
ans[
20
];
bool
cmp(
const
char
&
a,
const
char
&
b)
{
int
x
=
a
–
‘
A
‘
;
int
y
=
b
–
‘
A
‘
;
if
(x
>=
26
)
x
-=
‘
a
‘
–
‘
A
‘
;
if
(y
>=
26
)
y
-=
‘
a
‘
–
‘
A
‘
;
if
(x
!=
y)
return
x
<
y;
return
a
<
b;
}
void
dfs(
int
a)
{
if
(a
==
len)
{
ans[a]
=
‘
\0
‘
;
printf(
“
%s\n
“
, ans);
return
;
}
for
(
int
i
=
0
; i
<
n; i
++
)
if
(num[i]
>
0
)
{
ans[a]
=
st[i];
num[i]
—
;
dfs(a
+
1
);
num[i]
++
;
}
}
int
main()
{
//
freopen(“t.txt”, “r”, stdin);
int
t;
scanf(
“
%d
“
,
&
t);
while
(t
—
)
{
scanf(
“
%s
“
, st);
sort(st, st
+
strlen(st), cmp);
memset(num,
0
,
sizeof
(num));
int
j
=
0
;
len
=
strlen(st);
for
(
int
i
=
0
; i
<
len; i
++
)
if
(st[i]
==
st[j])
num[j]
++
;
else
{
st[
++
j]
=
st[i];
num[j]
=
1
;
}
n
=
j
+
1
;
dfs(
0
);
}
return
0
;
}
转载于:https://www.cnblogs.com/rainydays/archive/2011/06/24/2089132.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110604.html原文链接:https://javaforall.net