Jump to content
v1tal1ty

strings-> ocorencias consecutivas

Recommended Posts

v1tal1ty

Boas, mais uma vez peco ajuda a P@P

tenho uma duvida em relacao a algoretmia de seguinte exemplo:

tenho uma string e tenho que encontrar a maior sequencia consecutiva do mesmo caracter...

isso e por exemplo:

AAAAaaBBccJFADFAF  --- nesse caso o output seria 4 A porque e case sensitive

BBBBBBHHAABBBB -- nesse caso seria 6 B mesmo sabendo que ha mais Bs( tem que ser consecutivos)

cumps e mais uma vez obrigado

Share this post


Link to post
Share on other sites
KTachyon

Usas dois contadores. Comparas os chars directamente:

char a; char b;
int cadeia;

(...)

if (a == b) cadeia++;

Quando a cadeia ultrapassar o tamanho a cadeia máxima encontrada anteriormente, guardas o char e a contagem noutra variável.


“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Share this post


Link to post
Share on other sites
v1tal1ty

E um bocado dificil perceber o teu exemplo, mas sim ja me deste umas pistas, vou utilizar um array de estruturas para guardar la as cadeias....

Share this post


Link to post
Share on other sites
Localhost

Estruturas para resolver este problema...? Onde tu já vais...

Faz algo como isto:

for (i = 0; my_string[i]; i++, cur_counter++)
    if (cur_char != my_string[i])
    {
        if (cur_counter > final_counter)
        {
            final_counter = cur_counter;
            final_char = cur_char; 
        }
        cur_char = my_string[i];
    }

printf ("%d%c\n", final_counter, final_char);

Vê se te agrada.


here since 2009

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.