P2580 So he started the wrong roll call (dictionary tree)

topic background

The coach of the XS middle school chemistry competition team is a hearthstone lover.

He would roll the roll while kneading the furnace stone, so that one day he called a certain classmate twice in a row, and then happened to be discovered by the passing principal, and then he had a meal of Ola Ola Ola (for details, please refer to the completed game CON900).

topic description

After this the headmaster appoints you as special agent to record his roll call every day. The principal will provide the number and list of students in the chemistry competition, and you need to tell the principal whether he has made a mistake. (Why not just stop him from playing Hearthstone.)

input format

The first line contains an integer n, representing the number of people in the class.

In the next n lines, each line has a string representing its name (different from each other, and only contains lowercase letters, and the length does not exceed 5050).

Line n+2 contains an integer m, indicating the number of names reported by the coach.

Next m lines, each line contains a string representing the name of the coach (only lowercase letters, and the length does not exceed 5050).

output format

For each coach's reported name, output one line.

If the name is correct and it is the first occurrence, output  OK, if the name is incorrect, output  WRONG, if the name is correct but not the first occurrence, output  REPEAT.

Input and output samples

Type #1 to copy

5  
a
b
c
ad
acd
3
a
a
e

output #1 copy

OK
REPEAT
WRONG

Solution one:

Use STL map

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
int n;
int main() {
	map<string, int> student;
	string name;
	cin >> n;
	while (n--) {
		cin >> name;
		student[name] = 1;
	}
	int m; cin >> m;
	while (m--) {
		cin >> name;
		if (student[name] == 1) { puts("OK"); student[name] = 2; }
		else if (student[name] == 2) puts("REPEAT");
		else    puts("WRONG");
	}
	return 0;
}

Solution two:

Dictionary tree application:

1. String retrieval; 2. Word frequency statistics 3. Dictionary order sorting 4. Prefix matching

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
const int N = 800000;
struct node {
	bool repeat; // 这个前缀是否重复
	int son[26]; //26个字母
	int num; // 这个前缀出现的次数
}t[N];
int cnt = 1;  // 当前新分配的存储位置,把cnt = 0 留给根节点
void Insert(char* s) {
	int now = 0;
	for (int i = 0; s[i]; i++) {
		int ch = s[i] - 'a';
		if (t[now].son[ch] == 0) {
			t[now].son[ch] = cnt++;
		}
		now = t[now].son[ch];
		t[now].num++;
	}
}
int Find(char* s) {
	int now = 0;
	for (int i = 0; s[i]; i++) {
		int ch = s[i] - 'a';
		if (t[now].son[ch] == 0) return 3;
		now = t[now].son[ch];
	}
	if (t[now].num == 0) return 3; // 这个前缀没有出现过
	if (t[now].repeat == false) {
		t[now].repeat = true;
		return 1;
	}
	return 2;
}
int main() {
	char s[51];
	int n;
	cin >> n;
	while (n--) {
		scanf("%s", s);
		Insert(s);
	}
	int m;
	scanf("%d", &m);
	while (m--) {
		scanf("%s", s);
		int r = Find(s);
		if (r == 1) { puts("OK"); }
		else if (r == 2) { puts("REPEAT");}
		else { puts("WRONG"); }
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/zhi6fui/article/details/128735353