We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Recursion
- Password Cracker
- Discussions
Password Cracker
Password Cracker
Sort by
recency
|
134 Discussions
|
Please Login in order to post a comment
This problem is shit because of the test cases. You don't need memoization or dynamic programming. It is quite simple with tail recursion
`python def passwordCracker(passwords, login, res=''): if login == '': return res match = next((p for p in passwords if login[0:len(p)] == p), None)
# Trampoline to simulate tail calls in Python result, args = passwordCracker, (passwords, loginAttempt) while callable(result): r = passwordCracker(*args) try: result, args = r except: result = r `
def find(word,lis,rlis): if word in lis: rlis.append(word) return rlis if not word: rlis.append('W') return rlis if len(word) == 1: rlis.append('W') return rlis head, tail = [],[] for i in range(1,len(word)): h,t = word[:i],word[i:] if h in lis: head.append(h) tail.append(t) # print(head, tail) if not head: rlis.append('W') return rlis for i in range(len(head)): t = find(tail[i],lis,[]) if "W" not in t: rlis.append(head[i]) rlis += t return rlis rlis.append('W') return rlis def passwordCracker(passwords, loginAttempt): usepass = [] for p in passwords: rest = [i for i in passwords if i != p] if find(p,rest,[]) == ['W']: usepass.append(p) attset = {i for i in loginAttempt} passset = set() for i in usepass: passset.update({j for j in i}) if len(attset - passset) > 0: return 'WRONG PASSWORD' # la = re.findall(r'^(\w+?)\1*$', loginAttempt)[0] #print(la) # count = len(loginAttempt)//len(la) sys.setrecursionlimit(3000) r = find(loginAttempt,usepass,[]) if r == ['W']: return 'WRONG PASSWORD' else: s = '' for i in r: s += i + ' ' return s.rstrip(' ')
C++
I get infiite recursion. Please explain why
why my code is wrong
include using namespace std; const int MAX_N = 1000; int count1[MAX_N]; int j = 0; bool ok(string s, string a[], int n, int x){ if(x >= s.size()) return true; for(int i = 0; i < n; i++){ if(s.substr(x, a[i].size()) == a[i]){ count1[j++] = i; if(ok(s, a, n, x + a[i].size())) return true; j--; } } return false; } int main(){ int t; cin >> t; while(t--){ memset(count1, -1, sizeof(count1)); int n; cin >> n; string a[n]; for(int i = 0; i < n; i++){ cin >> a[i]; } string s; cin >> s; if(!ok(s, a, n, 0)) cout << "WRONG PASSWORD\n"; else { for(int i = 0; count1[i] != -1; i++){ cout << a[count1[i]] << " "; } cout << endl; } j = 0; } return 0; }