Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

permutacija stringova

[es] :: Java :: permutacija stringova

[ Pregleda: 2343 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

mstaletovic
Graz

Član broj: 204405
Poruke: 3
*.dynamic.xdsl-line.inode.at.



Profil

icon permutacija stringova10.12.2008. u 23:03 - pre 187 meseci
Sledeci zadatak ne znam kako da uopste pocnem(pocetnik sam):

Trebam da napravim klasu Permutations sa statickom metodom:

static ArrayList<String>getAllPermutations(String[] array), pomocu koje vracamo sve stringove u jednu ArrayList-u, bez duplog pojavljivanja.
Primer: getAllPermutations({"aba", "ab", "a"}). Rezultat bi trebao biti: "abaaba", "abaaab", "aabaab", "aababa".

Pozdrav i hvala unapred,
milan
 
Odgovor na temu

Ivan Ivanic
Ivan Ivanic
Freelance, Anywhere
Fruška Gora

Član broj: 203038
Poruke: 179
*.opera-mini.net.

Sajt: ivan.yggdrasillcode.com


Profil

icon Re: permutacija stringova11.12.2008. u 07:46 - pre 187 meseci
Je si li siguran da su ovo sve jedinstvene permutacije ovih Stringova?
Rad rad i samo rad :-D
 
Odgovor na temu

mstaletovic
Graz

Član broj: 204405
Poruke: 3
*.dynamic.xdsl-line.inode.at.



Profil

icon Re: permutacija stringova11.12.2008. u 13:35 - pre 187 meseci
Da. Te permutacije su navedene u postavci zadatka kao moguce resenje. Naravno mogu se izabrati i neki drugi stringovi.
 
Odgovor na temu

Ivan Ivanic
Ivan Ivanic
Freelance, Anywhere
Fruška Gora

Član broj: 203038
Poruke: 179
*.opera-mini.net.

Sajt: ivan.yggdrasillcode.com


Profil

icon Re: permutacija stringova11.12.2008. u 14:12 - pre 187 meseci
A je li broj stringova fiksno 3? Pitam te zato što je rešenje lakše ako jeste ;)
Rad rad i samo rad :-D
 
Odgovor na temu

mstaletovic
Graz

Član broj: 204405
Poruke: 3
*.dynamic.xdsl-line.inode.at.



Profil

icon Re: permutacija stringova11.12.2008. u 15:39 - pre 187 meseci
Nije navedeno koliko stringova, ali pretpostavljam da moze 3. Hvala! :)


 
Odgovor na temu

Ivan Ivanic
Ivan Ivanic
Freelance, Anywhere
Fruška Gora

Član broj: 203038
Poruke: 179
*.opera-mini.net.

Sajt: ivan.yggdrasillcode.com


Profil

icon Re: permutacija stringova11.12.2008. u 17:27 - pre 187 meseci
Evo ti moj kod koji sam radio kad sam tako nešto isprobavao pa probaj da ga malo
prilagodiš svom zadatku. Pretpostavljam da će ti dati neku ideju ;)
Aj nek ti je sa srećom :D

Code:

import java.util.*;

public class Main{
    public static void main(String[] args){
        String[] source = {"a", "aba", "ab"};
        ArrayList<String> result = Permutations.getPermutations(source);
        for(String s : result){
            System.out.print(s + " ");
        }
        System.out.println();
    }
}

class Permutations{
    public static ArrayList<String> getPermutations(String[] source){
        ArrayList<String> list = new ArrayList<String>();
        /* set assures to have only unique values */
        Set<String> set = new TreeSet<String>();
        
        /* loop for creating family of strings that start with same string */
        for(int i = 0; i < source.length; i++){
            /* initialize start of resulting String */
            String temp = source[i];
            
            /* contains number from which to add  next strings in array */
            int nextStart = i + 1;
            if(nextStart == source.length){
                nextStart = 0;
            }
            
            /* loop to make all Strings in family of strings
             * that start with source[i] */ 
            for(int k = 0; k < source.length; k++){
                
                int stringToAdd = nextStart;
                
                /* loop to add all consecutive strings to temp, if it reaches
                 * end of array starts from begining of array. Make 1 itteration 
                 * less then number of Strings in array, because
                 * we have already used one String */
                for(int j = 0; j < source.length - 1; j++){
                    System.out.print("for " + stringToAdd + " ");
                    /* if next string index is equal to array length */
                    if(stringToAdd >= source.length){
                        System.out.print("if " + stringToAdd + " ");
                        stringToAdd = 0;
                        System.out.print("ifchanged " + stringToAdd + " ");
                    }
                    if(stringToAdd == i){
                        stringToAdd++;
                    }
                    if(stringToAdd >= source.length){
                        stringToAdd = 0;
                    }
                    System.out.print("before temp " + stringToAdd + " ");
                    
                    temp += source[stringToAdd];
                    stringToAdd++;
                }
                System.out.println();
                nextStart++;
                set.add(temp);
                temp = source[i];
            }
            
            
        }
        
        list.addAll(set);
        return list;
    }
}

Rad rad i samo rad :-D
 
Odgovor na temu

[es] :: Java :: permutacija stringova

[ Pregleda: 2343 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.