Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Лозинки Problem 1 (3 / 3)
- Потребно е да се симулира најава на еден систем. Притоа корисникот внесува корисничко име и лозинка. Доколку корисничкото име одговара со лозинката тогаш се печати Najaven, доколку не одговара се печати Nenajaven и на корисникот му се дава повторна шанса на корисникот да внесе корисничко име и лозинка. Во моментот кога корисникот ќе биде најавен престануваат обидите за најава.
- Влез: Прво се дава број N на кориснички имиња и лозинки кои ќе бидат внесени во системот. Во наредните N реда се дадени корисничките имиња и лозинки разделени со едно празно место. Потоа се даваат редови со кориснички имиња и лозинки на корисници кои се обидувата да се најават (Пр. ana banana) За означување на крај на обидите во редицата се дава зборот KRAJ
- Излез: За секој од влезовите кои се обид за најава се печати Nenajaven се додека не дoбиеме Najaven или додека имаме обиди за најава.
- Пример. Влез: 3 ana banana pero zdero trpe trpi ana ana ana banana trpe trpi KRAJ
- Излез: Nenajaven Najaven
- Забелешка: Работете со хеш табела со затворени кофички. Самите решавате за големината на хеш табела, а хеш функцијата ви е дадена.
- Име на класа: Lozinki
- // 1
- /*import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Hashtable;
- public class Lozinki {
- public static void main (String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- Hashtable<String, String> map = new Hashtable<>();
- for(int i=1;i<=N;i++){
- String imelozinka = br.readLine();
- String[] pom = imelozinka.split(" ");
- String name = pom[0];
- String password = pom[1];
- map.put(name, password);
- }
- while (true) {
- String line = br.readLine();
- if (line.equals("KRAJ"))
- break;
- String parts[] = line.split(" ");
- String name = parts[0];
- String password = parts[1];
- if (map.containsKey(name)) {
- if (map.get(name).equals(password)) {
- System.out.println("Najaven");
- break;
- }
- else
- System.out.println("Nenajaven");
- }
- else
- System.out.println("Nenajaven");
- }
- }
- }*/
- // 2
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Lozinki {
- public static void main (String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- CBHT<String,String> table = new CBHT<>(2*N);
- for(int i=1;i<=N;i++){
- String imelozinka = br.readLine();
- String[] pom = imelozinka.split(" ");
- table.insert(pom[0], pom[1]);
- }
- while (true) {
- String line = br.readLine();
- if (line.equals("KRAJ"))
- break;
- String parts [] = line.split(" ");
- SLLNode<MapEntry<String,String>> temp = table.search(parts[0]);
- if (temp == null || !(temp.getElement().value.equals(parts[1])))
- System.out.println("Nenajaven");
- else {
- System.out.println("Najaven");
- break;
- }
- }
- }
- }
- class CBHT<K extends Comparable<K>,E> {
- private SLLNode<MapEntry<K,E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K,E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- SLLNode<MapEntry<K,E>> curr = buckets[b];
- for (; curr != null; curr = curr.succ) {
- if (targetKey.equals(curr.getElement().key)) {
- return curr;
- }
- }
- return null;
- }
- public void insert(K key, E value) {
- MapEntry<K,E> newEntry = new MapEntry<K,E>(key, value);
- int b = hash(key);
- SLLNode<MapEntry<K,E>> curr = buckets[b];
- for (; curr != null; curr = curr.succ) {
- if (key.equals(curr.getElement().key)) {
- curr.setElement(newEntry);
- return;
- }
- }
- buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
- }
- public void delete (K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- int b = hash(key);
- SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b];
- for (; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(curr.getElement().key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.getElement().toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- K key;
- E value;
- public MapEntry(K key, E value) {
- this.key = key;
- this.value = value;
- }
- @Override
- public int compareTo(K that) {
- return this.key.compareTo(that);
- }
- @Override
- public String toString () {
- return "<" + key + "," + value + ">";
- }
- }
- class SLLNode<E> {
- private E element;
- public SLLNode<E> succ;
- public SLLNode(E element, SLLNode<E> succ) {
- this.setElement(element);
- this.succ = succ;
- }
- @Override
- public String toString() {
- return getElement().toString();
- }
- public E getElement() {
- return element;
- }
- public void setElement(E element) {
- this.element = element;
- }
- }
- Sample input
- 3
- ana banana
- pero zdero
- trpe trpi
- ana ana
- ana bannana
- trpe trpe
- KRAJ
- Sample output
- Nenajaven
- Nenajaven
- Nenajaven
Add Comment
Please, Sign In to add comment