Bank and vulnerable passwords
|
All submissions for this problem are available.
There is a bank which gives their customers a password 'only' for transactions(there is no username) via ATM. Also the ATM does not have any enter key, it automatically logs you into your account as soon as you enter the right password. There is a huge problem in such type of system,i.e., suppose that if two people A and B have “aadil” and “aadilahmad” as their passwords respectively, so whenever B will try to login, the machine will automatically login B into A’s account.
Input:
First line of the input contains an integer N,the number of users in Bank. Next N lines contains strings which are passwords of the users. Tell if the bank is vulnerable or non-vulnerable.
First line of the input contains an integer N,the number of users in Bank. Next N lines contains strings which are passwords of the users. Tell if the bank is vulnerable or non-vulnerable.
Output:
Print "vulnerable" if the bank is vulnerable or "non vulnerable" if the bank is not vulnerable.
Print "vulnerable" if the bank is vulnerable or "non vulnerable" if the bank is not vulnerable.
Program constraints -
N <= 10^5
All passwords consists of lower case english alphabets
1<=length of password<=50
N <= 10^5
All passwords consists of lower case english alphabets
1<=length of password<=50
Sample Input -
2
likemeifyoucan
likeme
2
likemeifyoucan
likeme
Sample Output -
vulnerable
vulnerable
--------------------------------------------------------------editorial------------------------------------------------------------------------------
DIFFICULTY:
EASY-MEDIUM
PREREQUISITES:
Trie
PROBLEM:
Problem: Given a list of passwords (word) you need to tell whether any word is the prefix of any other word in the list.
EXPLANATION:
First of all I would like to enlighten that some of the contestents have solved the problem by just sorting them and finding the substring due to weak test cases. But the problem was intended to be solved using the data structure trie. You are given a list of words; you need to find if any word is the prefix of another word in the list. You can do so by inserting each given passwords in the trie and simultaneously checking if insertion of the password makes conflict with the condition, if so then print “vulnerable” else print “non vulnerable”. Remember to scan all the words even if the conflict occurs in between.
--------------------------------------------------code-----------------------------------------------------------------------
- #include<stdio.h>
- #include<string.h>
- #include<iostream>
- using namespace std;
- struct node
- {
- int word_count;
- struct node * has[26];
- bool ispre[26];
- };
- int exist=0;
- node* init()
- {
- node *node1=new node;
- node1->word_count=0;
- for(int i=0;i<26;i++){
- node1->has[i]=NULL;
- node1->ispre[i]=false;
- //cout<<"here"<<endl;}
- }
- return node1;
- }
- node *root;
- void build(char arr[])
- {
- node *present=root;
- // cout <<arr<<endl;
- for(int i=0;i<len;i++)
- {
- // cout<<"building "<<endl;
- int flag=0;
- if(present->has[arr[i]-'a']==NULL)
- {
- // cout<<" creating "<<arr[i]<<endl;
- flag=1;
- ////cout<<"making"<<endl;
- present->has[arr[i]-'a']=init();
- }
- if(present->ispre[arr[i]-'a']==true)
- {
- exist=1;
- // cout<<"herer "<<endl;
- }
- if(i==len-1)
- {
- // cout<<"word formed with "<<arr[i]<<endl;
- present->ispre[arr[i]-'a']=true;
- }present=present->has[arr[i]-'a'];
- if(i==len-1 && flag==0)
- {
- exist=1;
- }
- }
- }
- int main()
- {
- int t;
- cin>>t;
- node *node1=init();
- root=node1;
- while(t--)
- {
- char arr[100000];
- cin>>arr;
- build(arr);
- }
- if(exist==1)
- {
- cout<<"vulnerable"<<endl;
- }
- else
- cout<<"non vulnerable"<<endl;
- }