主题:[原创]pku acm 1250 Tanning Salon
一开始用栈,wrong answer。才发现并不满足先进先出,于是改用特征向量eigenvector。
# include <iostream>
# include <string>
using namespace std;
int tanned(string c, const int beds)
{
int current_num = 0, loss = 0;
bool eigenvector[26];
memset (eigenvector, false, 26);
for (int i = 0; i < c.length(); ++i) {
if (eigenvector[c[i] - 'A']) {
--current_num;
} else {
eigenvector[c[i] - 'A'] = true;
++current_num;
if (current_num > beds) {
++loss;
}
}
}
return loss;
}
int main()
{
int n;
while (cin >> n) {
if (n == 0) {
break;
}
string customers;
cin >> customers;
int loss = tanned(customers, n);
if (loss == 0) {
cout << "All customers tanned successfully." << endl;
} else {
cout << loss << " customer(s) walked away." << endl;
}
}
return 0;
}
[url=http://blog.csdn.net/Sedgewick]感兴趣的来我的窝看看。[/url]
# include <iostream>
# include <string>
using namespace std;
int tanned(string c, const int beds)
{
int current_num = 0, loss = 0;
bool eigenvector[26];
memset (eigenvector, false, 26);
for (int i = 0; i < c.length(); ++i) {
if (eigenvector[c[i] - 'A']) {
--current_num;
} else {
eigenvector[c[i] - 'A'] = true;
++current_num;
if (current_num > beds) {
++loss;
}
}
}
return loss;
}
int main()
{
int n;
while (cin >> n) {
if (n == 0) {
break;
}
string customers;
cin >> customers;
int loss = tanned(customers, n);
if (loss == 0) {
cout << "All customers tanned successfully." << endl;
} else {
cout << loss << " customer(s) walked away." << endl;
}
}
return 0;
}
[url=http://blog.csdn.net/Sedgewick]感兴趣的来我的窝看看。[/url]