Ragnarok
BAN USERBeginner
public static void listStrings(byte[] x, int num, int sb_index, StringBuffer sb)
{
if( num >= x.length )
{
return;
}
sb.append((char)x[num]);
System.out.println(sb.toString());
for(int i=num+1;i<x.length;i++)
{
listStrings(x,i,sb_index+1,sb);
}
sb.setLength(sb_index);
}
public static void main(String[] arg)
{
byte x[] = "abc".getBytes();
StringBuffer sb = new StringBuffer();
for(int i=0;i<x.length;i++)
{
listStrings(x,i,0,sb);
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public double value;
public TreeNode left,right;
public TreeNode(double v)
{
value = v;
}
}
class BST
{
TreeNode root = null;
public void insert(double x)
{
if( root == null )
{
root = new TreeNode(x);
return;
}
TreeNode current = root;
TreeNode parent = root;
while(current!=null)
{
parent = current;
if( current.value > x )
{
current = current.left;
}else
{
current = current.right;
}
}
if( parent.value > x )
{
parent.left = new TreeNode(x);
}else
{
parent.right = new TreeNode(x);
}
}
public double findCloset(double [] array, int key)
{
for(double x : array)
{
insert(x);
}
//tree built
TreeNode t = root;
while(t!=null)
{
double x = t.value;
if( x > key )
{
if(t.left != null && Math.abs(t.left.value - key) < Math.abs(x - key))
{
t = t.left;
}else
{
return t.value;
}
}else
{
if(t.right != null && Math.abs(t.right.value - key) < Math.abs(x - key))
{
t = t.right;
}else
{
return t.value;
}
}
}
return 0;
}
}
public class findArray
{
public static void main(String[] arg)
{
BST b = new BST();
double []v = {1.2,3.4,5.6,1,4.5,6.7,3.2,2.9,3.1};
System.out.println(b.findCloset(v, 3));
}
}
import java.util.Arrays;
class TreeNode
{
public int value;
public int count;
public TreeNode left,right;
public TreeNode(int v)
{
value = v;
count = 0;
}
}
class BST
{
TreeNode root = null;
private int findAndInsert(int value,int all)
{
if( root == null)
{
root = new TreeNode(value);
return 0;
}
if( root.value < value)
{
TreeNode node = new TreeNode(value);
node.left = root;
node.count = all - 1;
root = node;
return all - 1;
}
TreeNode current = root;
TreeNode parent = root;
while(current != null)
{
if( current.value > value )
{
current.count++;
parent = current;
current = current.left;
}else
{
break;
}
}
TreeNode node = new TreeNode(value);
node.count = parent.count -1;
node.left = parent.left;
parent.left = node;
return node.count;
}
public int[] find(int[] a)
{
BST tree = new BST();
for(int i=a.length - 1; i> -1; i--)
{
a[i] = tree.findAndInsert(a[i], a.length - i);
}
return a;
}
}
public void _find(int a[], int used[], int v, int sum , int current)
{
if( current > a.length )
{
return;
}
sum += a[current];
if( sum == v )
{
System.out.println(Arrays.toString(used));
return;
}else if( sum > v)
{
return;
}
for( int i = current+1; i < a.length ; i++ )
{
if( used[i] == 0 )
{ used[i] = 1;_find(a,used,v,sum,i);used[i] = 0; }
}
}
public void findSubset(int a[], int v)
{
Arrays.sort(a);
int used[] = new int[a.length];
System.out.println(Arrays.toString(a));
for(int i=0;i<a.length;i++)
{
used[i] = 1;
_find(a,used,v,0,i);
used[i] = 0;
}
}
public void shirft(int []num)
{
int pointer,i;
pointer = num.length-2;
for( i = num.length-1 ; i >-1; i-- )
{
if(i<pointer)
pointer = i-1;
if(num[i] == 0)
{
while(pointer > -1 && num[pointer] == 0)
pointer--;
if(pointer < 0)
break;
num[i] = num[pointer];
num[pointer] = 0;
pointer--;
}
}
System.out.println(Arrays.toString(num));
}
using a hash table
scan from left to right, if the table doesn't contain current element, put it in; if it does, just return
-----------------------------------------
Sorry I took the question as "find the first duplicated char".....
as what @Saam had said, using a linkedhashMap will reduce the search time of second round
In fact, I think @Saam 's answer is good enough, but if we want to scan once, we can do this. The solution below is really trivial, while we cost much more space.
while we still have the table[256](or hashMap) to count each char's occurrence frequency, We create a linked list T, each node contains the char that is unique, while use a hashtable<char,Node> to translate the value to the node in T instantly.
Then we need only one round, check if the current char never appeared( table[current] == 0 ), add a node to T, and put it into the hashtable, then table[current]++;
if tab[current] > 0, find if the node contains the element exists using the hashtable, remove the node from T.
Then the first element of T would be the answer when first round ends.
//took about O(N) where N is the length of the long string
//each word in String[] v must be different!
public void findString(String L,String[] v)
{
Hashtable<String,Integer> table = new Hashtable<String,Integer>();
int all = 0;
for(int i=0;i<v.length;i++)
{
table.put(v[i], i+1);
all += i+1;
}
int sizeV = v[0].length();
int sizeVL = v.length;
int [] o = new int[L.length() - sizeV + 1];
StringBuffer sb = new StringBuffer();
for(int i=0;i<o.length;i++)
{
sb.setLength(0);
for(int j=0;j<v[0].length();j++)
sb.append(L.charAt(i+j));
if( table.containsKey(sb.toString()) )
{
o[i] = table.get(sb.toString());
}else
{
o[i] = 0;
}
}
for(int i=sizeVL*sizeV;i<o.length;i++)
{
int sum = o[i];
for(int j=1;j<sizeVL;j++)
{
sum += o[i - sizeV*j];
}
if( sum == all )
{
System.out.println("find at" + (i - v.length*sizeV));
System.out.println(L.subSequence(i-v.length*sizeV, i+sizeV));
return;
}
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
public void parseJSON()
{
byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
ArrayList<UniType> array = new ArrayList<UniType>();
int type = 0;// 0 for array, 1 for object
int status = 0;
byte[] trues = "true".getBytes();
byte[] falses = "false".getBytes();
UniType temp = new UniType();
for(int i = 0; status!=-1 && i < input.length ; i++)
{
char c = (char) input[i];
switch(status)
{
case 0:
if(c == '{')
status = 1;
else
status = -1;
break;
case 1:
switch(c)
{
case ' ':
break;
case '"':
status = 2;
temp.type = 1;
temp.value.append('"');
break;
case '+':
case '-':
temp.type = 0;
temp.value.append(c);
status = 4;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp.type = 0;
temp.value.append(c);
status = 5;
break;
case 'f':
temp.type = 2;
temp.value.append(c);
status = 10;
break;
case 't':
temp.type = 2;
temp.value.append(c);
status = 6;
break;
default:
status = -1;
}
break;
case 2:
if(c == '"')
{
temp.value.append('"');
array.add(temp);
status = 3;
temp = new UniType();
}else
{
temp.value.append(c);
}
break;
case 3:
switch(c)
{
case ':':
type = 1;
case ',':
status = 1;
break;
case '}':
status = -1;
default:
status = -1;
}
break;
case 4:
if( c >= '0' && c <= '9' )
{
status = 5;
temp.value.append(c);
}else
{
status = -1;
}
break;
case 5:
if( c >= '0' && c <= '9' )
{
temp.value.append(c);
}else if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 6:
case 7:
case 8:
if(c == trues[status - 5])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 9:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
case 10:
case 11:
case 12:
case 13:
if(c == falses[status - 9])
{
temp.value.append(c);
status++;
}else
{
status = -1;
}
break;
case 14:
if(c == ',')
{
array.add(temp);
temp = new UniType();
status = 1;
}else if(c == '}')
{
array.add(temp);
temp = new UniType();
status = -1;
}else
status = -1;
break;
default:
status = -1;
}
}
System.out.println(type == 1 ? "object" : "array");
//print out the result
status = 0;
for(UniType un : array)
{
if(status < type)
System.out.print( un.getValue() + ":");
else
System.out.println(un.value.toString());
status = 1 - status;
}
}
a recursive version that may be wrong
public boolean compare(byte[] rule, byte[] string, int p_r, int p_s)
{
if(p_s >= string.length)
{
while(p_r<rule.length)
if(rule[p_r++]!= '*')
return false;
return true;
}else if(p_r >= rule.length)
{
return false;
}
if(rule[p_r] == '*')
{
return p_r == rule.length - 1 ? true : compare(rule,string,p_r,p_s + 1) || compare(rule,string,p_r + 1,p_s);
}else if(rule[p_r] == '.' || string[p_s] == rule[p_r])
{
return compare(rule,string,p_r+1,p_s + 1);
}else
{
return false;
}
}
int [] arr = {0,0,0,0,2,1,2,0,2,1,2,1,2,0,1,0,1,0,2,0,1};
int pointer1 = 0,pointer2 = arr.length - 1;
for(int i = 0;i<pointer2;)
{
if(arr[i]==0)
{
if(i == pointer1)
{//do this to skip the worst case
i++;
pointer1++;
}else
{
arr[i] = arr[pointer1];
arr[pointer1++] = 0;
}
}else if( arr[i] == 2 )
{
arr[i] = arr[pointer2];
arr[pointer2--] = 2;
}else
{
i++;
}
}
System.out.println(Arrays.toString(arr));
int[] arrs = {0,0,1,0,0,1,1,1,1,0}; //prepend a zero to the array
int i = 0;
HashMap<Integer,Stack<Integer>> hm = new HashMap<Integer,Stack<Integer>>();
Stack<Integer> stk;
stk = new Stack<Integer>();
stk.push(0);
hm.put(0, stk);
for(i = 1; i<arrs.length ; i++)
{//count from zero
if(arrs[i] == 0)
{
arrs[i] = -1;
}
arrs[i] += arrs[i-1];
if(!hm.containsKey(arrs[i]))
{
stk = new Stack<Integer>();
hm.put(arrs[i], stk);
}else
{
stk = hm.get(arrs[i]);
}
stk.push(i); // in fact, we can push the item in an increasing order
}
for(i = 1; i < arrs.length; i++)
{
stk = hm.get(arrs[i]);
for(Integer x : stk)
{
if(x < i)
{
System.out.println( "( " + x + " , " + (i-1) + " )" );
}
}
}
count in O(n)
while output in almost O(n^2)
//a really dumb solution using DP
public class Test {
String needTest = "cab";
int sum = 'a' + 'b' + 'c';
Test()
{
System.out.println(testString(needTest));
}
int testString(String s)
{
int min = s.length();
if(min == 1)
{
return 1;
}else if(min == 2)
{
if(s.charAt(0) == s.charAt(1))
{
return 2;
}else
{
return 1;
}
}
StringBuffer sb = new StringBuffer();
for(int i= 1; i<s.length() ; i++)
{
if(s.charAt(i) != s.charAt(i-1))
{
sb = new StringBuffer();
sb.append(s.substring(0, i-1));
sb.append((char)(sum - s.charAt(i) - s.charAt(i-1)));
if(i<s.length()-1)
{
sb.append(s.substring(i+1));
}
int temp = testString(sb.toString());
if(min > temp)
{
min = temp;
}
}
}
return min;
}
public static void main(String[] argv)
{
Test t = new Test();
}
}
#include "stdio.h"
#include "string.h"
int main(int argc, char** argv)
{
int j=0,i=0;
char str[] = "4678912356012356";
char str1[80];
char temp = str[0];
for(i=1;i<strlen(str);i++)
{
if(str[i] != temp+1)
{
printf("%c;",temp);
}else
{
printf("%c",temp);
}
temp = str[i];
}
i-=2;
if(temp != str[i]+1)
{
printf(";%c",temp);
}else
{
printf("%c",temp);
}
return 0;
}
I'm not sure whether this would work.
- Ragnarok March 12, 2012