A common security method used for online banking is to ask the user for three ra
ID: 3582890 • Letter: A
Question
A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.
The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.
_____________________________
i want to code it with java using Graphs.
Explanation / Answer
This is a well known problem of Euler79. that is as described below:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.util.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.Map;
import java.util.Set;
public class Security {
public static final String File ="path to your file";
public void Passcode(){
List<int[]> dataList = readInts();
Set<Edge> set = new HashSet<Edge>();
for(int i=0;i<dataList.size();i++){
int[]tp = dataList.get(i);
{
Edge e1 = new Edge(tp[0], tp[1]);
Edge e2 = new Edge(tp[1],tp[2]);
set.add(e1);
set.add(e2);
}
}
TopoSort topsort = new TopoSort();
List<Integer> orders = topSort.topSort(set);
for(int i=0;i<orders.size();i++){
System.out.prinltn(orders.get(i));
}
}
public class TopoSort{
public List<Integer> topSort(Set<Edge>set){
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(Edge edge : set)
{
int to = edge.to;
int from = edge.from;
if(!map.containsKey(from)) {
map.put(from,0);
}
if(!map.containsKey(to)){
map.put(to,0);
}
map.put(to,map.get(to)+1);
}
List<Integer> res = new ArrayList<Integer>();
Set<Edge> cp = new HashSet<Edge>(set);
while(true)
{
int from = DegreeZero(cp,map);
cp = rmEdge(from,cp,map);
res.add(from);
if(cp.size()<1){
res.add(Integer.parseInt(map.keySet().toArray()[0].toString()));
break;
}
if(DegreeZero(cp,map) == -1 && cp.size() < 1){
throw new RuntimeException("Cycle occured");
}
}
return res;
}
private Set<Edge> rmEdge(int from, Set<Edge>cp,Map<Integer,Integer>map)
{
Set<Edge> res = new HashSet<Edge>();
for (Edge _edge : cp){
if(_edge.from !=from) {
res.add(_edge);
}
else {
map.put(_egde.to,map.get(_edge.to)-1);
}
}
map.remove(from);
return res;
}
private int DegreeZero(Set <Edge> cp, Map<Integer,Integer>map)
{
for(Edge edge : cp){
if(map.get(edge.from)==0){
return edge.from;
}
}
return -1;
}
}
public static class Edge{
public int from;
public int to;
public Edge(int from,int to){
super();
this.from = from;
this.to = to;
}
@Override
public String toString(){
return "Edge [from =" + from + ", to="+to+"]";
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + from;
result = prime * result + to;
result result;
}
@Override
public boolean equals(Object obj) {
if(this = obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Edge Other = (Edge) obj;
return other.from = from && other.to == to;
}
}
List<int []> readInts(){
try{
List<int[]> res = new ArrayList<int []>();
qBufferedReader reader = new BufferedReader (new InputStramReader)(new FileInputStream(FILE)));
String str = "";
while((str = reader.readLine()) != null) {
str = str.trim();
int[] tp = new int[3];
tp[0] = Integer.parseInt(String.valueOf(str.charAt(0)));
tp[1] = Integer.parseInt(String.valueOf(str.charAt(1)));
tp[2] = Integer.parseInt(String.valueOf(str.charAt(2)));
res.add(tp);
}
reader.close();
return res;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.