ACM ZOJ Problem Set - 1403 Safecracker
2014-07-28 01:25
309 查看
//虽然题目比较简单,但是这是我第一次使用dfs算法做这种题
//题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1403
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int b[5];
char c[5];
bool d[26];
string s;
int n;
int main()
{
int i, j;
bool f(int);
while (cin >> n >> s&&n != 0 && s != "END")
{
for (i = 1; i < s.length(); i++)
{
for (j = 0; j < s.length()-i; j++)
{
if (s[j] < s[j + 1])
{
char a = s[j];
s[j] = s[j + 1];
s[j + 1] = a;
}
}
}
for (i = 0; i < 26; i++)
{
d[i] = 0;
}
if (f(5))
{
for (i = 0; i < 5; i++)
{
c[i] = char('A' + b[i] - 1);
cout << c[i];
}
cout << endl;
}
else cout << "no solution" << endl;
}
return 0;
}
bool f(int m)
{
int i, j, k;
if (m == 0)
{
j = -1;
k = 0;
for (i = 0; i < 5; i++)
{
j = j *(-1);
k += j*(int)pow((double)b[i], (double)(i + 1));
}
if (k == n)return 1;
else return 0;
}
else
{
for (i = 0; i < s.length(); i++)
{
if (d[i] == 0)
{
b[5 - m] = (int)(s[i] - 'A' + 1);
d[i] = 1;
if (f(m - 1)) return 1;
else d[i] = 0;
}
}
return 0;
}
}
//虽然题目比较简单,但是这是我第一次使用dfs算法做这种题
//题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1403
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int b[5];
char c[5];
bool d[26];
string s;
int n;
int main()
{
int i, j;
bool f(int);
while (cin >> n >> s&&n != 0 && s != "END")
{
for (i = 1; i < s.length(); i++)
{
for (j = 0; j < s.length()-i; j++)
{
if (s[j] < s[j + 1])
{
char a = s[j];
s[j] = s[j + 1];
s[j + 1] = a;
}
}
}
for (i = 0; i < 26; i++)
{
d[i] = 0;
}
if (f(5))
{
for (i = 0; i < 5; i++)
{
c[i] = char('A' + b[i] - 1);
cout << c[i];
}
cout << endl;
}
else cout << "no solution" << endl;
}
return 0;
}
bool f(int m)
{
int i, j, k;
if (m == 0)
{
j = -1;
k = 0;
for (i = 0; i < 5; i++)
{
j = j *(-1);
k += j*(int)pow((double)b[i], (double)(i + 1));
}
if (k == n)return 1;
else return 0;
}
else
{
for (i = 0; i < s.length(); i++)
{
if (d[i] == 0)
{
b[5 - m] = (int)(s[i] - 'A' + 1);
d[i] = 1;
if (f(m - 1)) return 1;
else d[i] = 0;
}
}
return 0;
}
}
相关文章推荐
- ACM ZOJ Problem Set - 1002 fire net
- Zoj 3541 The Last Puzzle (dp) - 2011 ACM-ICPC Dalian Regional Contest Problem C
- 龙哥牛b代码!ZOJ Problem Set - 1649 Rescue
- ZOJ Problem Set - 1010
- ZOJ Problem Set - 2886 Look and Say
- ZOJ 1403&&HDU 1015 Safecracker【暴力】
- ZOJ Problem Set - 1095(打表)
- ZOJ Problem Set - 3811 Untrusted Patrol
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 3702 Gibonacci number
- ZOJ Problem Set - 3869||Ace of Aces
- ZOJ Problem Set - 1025
- ZOJ Problem Set - 1067
- ZOJ Problem Set - 2750
- ZOJ Problem Set - 1016 Parencodings
- ZOJ Problem Set - 1045 HangOver
- ZOJ Problem Set - 2812 Quicksum
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
- ZOJ Problem Set - 1008 Gnome Tetravex (TLE)
- ZOJ Problem Set - 1392 The Hardest Problem Ever