// 출력
System.out.println("...");
System.out.print("...");
// 1개
System.out.printf("... %d | %s",값1);
// 2개
System.out.printf("... %1$d %2$s",값1,값2,...);
// 입력
int keyCode = System.in.read();
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
class Node:
def __init__(self,text):
self.text = text
self.last = 0
self.output = ""
self.child = {}
self.fail = None
self.rank = 0
def show(self):
print( "text:", self.text, "output:",self.output, "fail:", self.fail.text if self.fail is not None else "None","rank:",self.rank)
if self.last:
print( "LAST OUTPUT : ", self.output)
for k in self.child.keys():
s = self.child[k]
s.show()
class AhocorasickTrie:
def __init__(self):
self.no = 0
self.root = Node(None)
def add(self,text):
rank = 0
currentNode = self.root
for t in text:
if t not in currentNode.child:
self.no +=1
currentNode.child[t] = Node(t)
print(f"ADD : {t} || Current No. : {self.no}")
currentNode.rank = rank
rank+=1
currentNode = currentNode.child[t]
currentNode.output = text
currentNode.last = 1
currentNode.rank = rank
print(f"TOTAL : {self.no}")
def set_failur(self):
queue = []
for c,node in self.root.child.items():
node.fail = self.root
queue.append(node)
while queue:
currentNode = queue.pop(0)
for t,node in currentNode.child.items():
fail_node = currentNode.fail
while fail_node and t not in fail_node.child:
fail_node = fail_node.fail
if fail_node:
node.fail = fail_node.child[t]
else:
node.fail = self.root
queue.append(node)
def show(self):
self.root.display()
def find(self,text):
result = []
currentNode = self.root
for t in text:
while currentNode is not None and t not in currentNode.child:
print("fail",currentNode.text)
currentNode = currentNode.fail
if t in currentNode.child:
currentNode = currentNode.child[t]
if currentNode.output != "":
result += [currentNode.output]
print(result,t,currentNode.output,currentNode.last,currentNode.output)
return result
class Node:
def __init__(self,text):
self.text = text
self.last = 0
self.child = {}
class Trie:
def __init__(self):
self.no = 0
self.root = Node(None)
def add(self,text):
currentNode = self.root
for t in text:
if t not in currentNode.child:
self.no +=1
currentNode.child[t] = Node(t)
print(f"ADD : {t} || Current No. : {self.no}")
currentNode = currentNode.child[t]
currentNode.last = 1
print(f"TOTAL : {self.no}")
def find(self,text):
result = ""
currentNode = self.root
for t in text:
if t in currentNode.child:
result += t
currentNode = currentNode.child[t]
return result