正经题解|指纹解锁
2024-05-27 16:24:00
发布于:浙江
48阅读
0回复
0点赞
题目分析
模拟
操作要求:
ADD
表示添加一个指纹,后面跟上指纹编码,如果指纹已经在库中,则输出already exists
, 否则视为添加成功输出success
。DEL
表示删除一个指纹,后面跟上要删除的指纹编码,如果指纹不在库中则输出non-existent
,否则视为删除成功输出success
。VIEW
查看当前所有的指纹编码,按照添加顺序换行显示,并在开头加上序号,从 开始。如果库中没有任何的指纹则输出empty
。UNLOCK
表示尝试解锁,后面跟上识别到的指纹编码,如果解锁成功输出YES
,失败输出NO
。
所有的指纹编码都是字符串,建一个字符串数组 。
可以再建一个标记数组 ,用于标记 的某个元素是否被删除了,如果 ,则说明 被删除了。
AC代码
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
string a[1010];
int removed[1010];
int ai = 0;
bool unlock(string x) {
for(int i=0;i<ai;i++) {
if (removed[i])continue;
if (a[i] == x) {
return true;
}
}
return false;
}
void add(string x) {
if (unlock(x)) {
cout << "already exists" << endl;
}else {
cout << "success" << endl;
a[ai++] = x;
}
}
void del(string x) {
for(int i=0;i<ai;i++) {
if (removed[i])continue;
if (a[i] == x) {
cout << "success" << endl;
removed[i] = 1;
return;
}
}
cout << "non-existent" << endl;
}
void view() {
int j = 1;
for(int i=0;i<ai;i++) {
if (removed[i])continue;
cout << j << ". " << a[i] << endl;
j++;
}
if (j == 1) {
cout << "empty" << endl;
}
}
int main() {
int n;
string op,x;
cin >> n;
for(int i=1;i<=n;i++) {
cin >> op;
if (op == "ADD") {
cin >> x;
add(x);
}
if (op == "DEL") {
cin >> x;
del(x);
}
if (op == "VIEW") {
view();
}
if (op == "UNLOCK") {
cin >> x;
if (unlock(x)) {
cout << "YES" << endl;
}else {
cout << "NO" << endl;
}
}
}
return 0;
}
复杂度分析
。
这里空空如也
有帮助,赞一个