String generieren

  • Themenstarter Themenstarter Baaron
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
B

Baaron

Gast
ein String A besteht aus n zeichen.

der zweite String B sollte verschiedene kombinationen aus "g,l,r" für jedes zeichen generiert werden. jedes zeichen kann g oder l oder r annehmen.
beispiel n=3

d.h. String B sollte verschiedenste Reihenfolgen annehmen ggg,ggl,ggr,rgl, usw...also er sollte nicht alle möglichkeiten durchgehen sondern zufällig irgendeine erzeugen.
kann man dem algo auch beibringen, dass er sich eine kombination merkt, wenn sie gut war? sobald im String A die folge kommt, für die er schon eine Kombination gemacht hatte, soll er die auch nehmen.

es wird nämlich anhand von der kombination im String B eine Matrix angelegt und mit Werten aus String A ausgefüllt.
g=grade,l=links,r=rechts. dort sollten bestimmte Sachen gezählt werden und wenn die anzahl der Treffer hoch ist, dann war die Kombination im STring B gut.

ich hoffe es gibt jemanden der sowas kann 🙂
ich nämlich nicht... 🙂 hab erst vor paar wochen mit java angefangen..und muss sowas realisieren...(steckt noch was dahinter) 🙂
 
Code:
public class Combinations {

    public static void comb1(String s) { comb1("", s); }

    private static void comb1(String prefix, String s) {
        if (s.length() > 0) {
            System.out.println(prefix + s.charAt(0));
            comb1(prefix + s.charAt(0), s.substring(1));
            comb1(prefix,               s.substring(1));
        }
    }  

    public static void comb2(String s) { comb2("", s); }
    private static void comb2(String prefix, String s) {
        System.out.println(prefix);
        for (int i = 0; i < s.length(); i++)
            comb2(prefix + s.charAt(i), s.substring(i + 1));
    }  


    public static void main(String[] args) {
       int N = 3;
       String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String elements = alphabet.substring(0, N);

       comb1(elements);
       System.out.println();

       comb2(elements);
       System.out.println();
    }

}

ist es das, was du wolltest?
 
ja so ähnlich
aber nicht
g
gl
glr
sondern
ggg
ggl
ggr
glg
glr
usw...aber halt random erzeugt..es sind sonst 3^3 kombinationen..wenn im String A dann 1000 zeichen sind, dann sind es 3^1000 🙂 und das dauert..also es sollte so 100 mal durchlaufen und dabei verschiedene kombinationen erzeugt werden
 
Ich habe da noch was von meiner Studienzeit gefunden. Es ist zwar etwas viel Code, aber den kannst du
problemlos umschreiben. Ich habe hier eine eigene Queue und einen eigenen Stack implementiert.
Du kannst natürlich auch den Stack resp. die Queue von Java nehmen, dann musst du einfach die print Methode neu schreiben.

Gruss

Code:
public class DequeEmptyException extends RuntimeException
{
  public DequeEmptyException(String err)
  {
    super(err);
  }
}

Code:
public class EmptyQueueException extends RuntimeException
{
  public EmptyQueueException(String err)
  {
    super(err);
  }
}

Code:
public class EmptyStackException extends RuntimeException
{
  public EmptyStackException(String err)
  {
    super(err);
  }
}

Code:
public class Permutationen
{
  public void permutationen(QueueImpl q, StackImpl s)
  {
    if (q.isEmpty())
    {
      System.out.print("Permutation : ");
      s.print();
      System.out.print("\n");
    }
    else
    {
      for (int i=1; i<=q.size(); i++)
      {
        s.push(q.dequeue());
        permutationen(q, s);
        q.enqueue(s.pop());
      }
    }
  }
  
  public static void main(String[] args)
  {
    Permutationen perm = new Permutationen();
    QueueImpl q = new QueueImpl();
    q.enqueue("a");
    q.enqueue("b");
    q.enqueue("c");
    q.enqueue("d");
    StackImpl s = new StackImpl();
    perm.permutationen(q,s);
  }
}

Code:
public interface Queue
{
 /**
  * Returns the number of elements in the queue.
  * @return number of elements in the queue.
  */
  public int size();
 /**
  * Returns whether the queue is empty.
  * @return true if the queue is empty, false otherwise.
  */
  public boolean isEmpty();
 /**
  * Inspects the element at the front of the queue.
  * @return element at the front of the queue.
  * @exception EmptyQueueException if the queue is empty.
  */
  public Object front() throws EmptyQueueException;
 /**
  * Inserts an element at the rear of the queue.
  * @param element new element to be inserted.
  */
  public void enqueue (Object element);
 /**
  * Removes the element at the front of the queue.
  * @return element removed.
  * @exception EmptyQueueException if the queue is empty.
  */
  public Object dequeue() throws EmptyQueueException;
}

Code:
public class QueueImpl implements Queue
{
  private Object[] array;
  private int size;
  
  public QueueImpl()
  {
    array = new Object[2];
    size = 0;
  }
  
  public int size()
  {
    return size;
  }

  public boolean isEmpty()
  {
    return (size == 0) ? true : false;
  }

  public Object front() throws EmptyQueueException
  {
    return array[0];
  }

  public void enqueue(Object element)
  {
    if (array.length <= size)
    {
      enlargeArray();
    }
    array[size++] = element;
  }

  public Object dequeue() throws EmptyQueueException
  {
    if (size > 0)
    {
	    Object obj = array[0];
	    for (int i=1; i<size; i++)
	    {
	      array[i-1] = array[i];
	    }
	    size--;
	    if((size >= 2) && (size <= array.length/2))
	    {
	      reduceArray();
	    }
	    return obj;
    }
    else
    {
      throw (new EmptyQueueException("Could not dequeue because queue is empty."));
    }
  }
  
  private void enlargeArray()
  {
    Object[] prev = array;
    array = new Object[prev.length*2];
    System.out.println("Enlarging array from "+prev.length+" to "+array.length);
    for (int i=0; i<prev.length; i++)
    {
      array[i] = prev[i];
    }
  }
  
  private void reduceArray()
  {
    Object[] prev = array;
    if (prev.length%2 == 0)
    {
      array = new Object[prev.length/2];
    }
    else
    {
      array = new Object[prev.length/2+1];
    }
    System.out.println("Reducing array from "+prev.length+" to "+array.length);
    for (int i=0; i<array.length; i++)
    {
      array[i] = prev[i];
    }
  }
  
  public void print()
  {
    if (size==0)
    {
      System.out.println("The queue is empty.");
    }
    else
    {
      for (int i=0; i<size; i++)
      {
        System.out.print(array[i]);
      }
    }
  }
  
  public static void main(String[] args)
  {
    QueueImpl queue = new QueueImpl();
    for (int i=0; i<20; i++)
    {
      System.out.println("enqueue(): "+i);
      queue.enqueue(new Integer(i));
    }
    System.out.println("front(): "+(Integer)queue.front());
    while (!queue.isEmpty())
    {
      System.out.println("dequeue(): "+(Integer)queue.dequeue());
    }
  }
}

Code:
public interface Stack
{
 /**
  * Return the number of elements in the stack.
  * @return number of elements in the stack.
  */
  public int size();
 /**
  * Return whether the stack is empty.
  * @return true if the stack is empty, false otherwise.
  */
  public boolean isEmpty();
 /**
  * Inspect the element at the top of the stack.
  * @return top element in the stack.
  * @exception EmptyStackException if the stack is empty.
  */
  public Object top() throws EmptyStackException;
 /**
  * Insert an element at the top of the stack.
  * @param element element to be inserted.
  */
  public void push (Object element);
 /**
  * Remove the top element from the stack.
  * @return element removed.
  * @exception EmptyStackException if the stack is empty.
  */
  public Object pop() throws EmptyStackException;
}

Code:
import java.util.LinkedList;

public class StackImpl implements Stack
{
  private LinkedList list;

  public StackImpl()
  {
    list = new LinkedList();
  }
  
  public int size()
  {
    return list.size();
  }

  public boolean isEmpty()
  {
    return list.isEmpty();
  }

  public Object top() throws EmptyStackException
  {
    try
    {
      return list.getLast();
    }
    catch (Exception ex)
    {
      throw (new EmptyStackException("Could not get top of stack because stack is empty."));
    }
  }

  public void push(Object element)
  {
    list.addLast(element);
  }

  public Object pop() throws EmptyStackException
  {
    try
    {
      return list.removeLast();
    }
    catch (Exception ex)
    {
      throw (new EmptyStackException("Could not remove top of stack because stack is empty."));
    }
  }
  
  public void print()
  {
    if (list.size()==0)
    {
      System.out.println("The stack is empty.");
    }
    else
    {
      for (int i=0; i<list.size(); i++)
      {
        System.out.print(list.get(i));
      }
    }
  }
  
  public static void main(String[] args)
  {
    StackImpl stack = new StackImpl();
    for (int i=0; i<20; i++)
    {
      System.out.println("push(): "+i);
      stack.push(new Integer(i));
    }
    System.out.println("top(): "+(Integer)stack.top());
    while (!stack.isEmpty())
    {
      System.out.println("pop(): "+(Integer)stack.pop());
    }
  }
}
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben