Ybadoo - Soluções em Software Livre
Tutoriais
Linguagens Formais e Autômatos

Desenvolva um programa, na linguagem de programação de sua preferência, que apresente os possíveis sufixos da palavra fornecida pelo usuário.

 

Terminal

ybadoo@server:~$ ./application
Diagrama de Classes
Diagrama de Classes da Implementação na Linguagem de Programação Java

Arquivo Suffix.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.lfa.tutorial01.topico07.exercicio03;

import java.util.LinkedList;
import java.util.List;

/**
 * Classe responsavel pela geracao dos sufixos da palavra
 */
public class Suffix
{
  /**
   * Lista de sufixos
   */
  private List<String> suffixes;

  /**
   * Palavra
   */
  private String word;

  /**
   * Construtor para inicializar a palavra
   *
   * @param word palavra
   */
  public Suffix(String word)
  {
    this.word = word;

    suffixes = new LinkedList<String>();

    suffixes.add("&");

    for(int index = word.length() - 1; index >= 0; index--)
    {
      suffixes.add(word.substring(index, word.length()));
    }
  }

  /**
   * Retornar true se o sufixo fornecido for um sufixo da palavra
   *
   * @param suffix sufixo fornecido
   * @return true se o sufixo fornecido for um sufixo da palavra
   */
  public boolean isSuffix(String suffix)
  {
    return suffixes.contains(suffix);
  }

  /**
   * Retornar a lista de sufixos
   *
   * @return lista de sufixos
   */
  public String[] list()
  {
    return suffixes.toArray(new String[0]);
  }

  /**
   * Retornar a quantidade de sufixos
   *
   * @return quantidade de sufixos
   */
  public int size()
  {
    return suffixes.size();
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    StringBuilder out = new StringBuilder();

    out.append("{");

    for(String suffix : suffixes)
    {
      if(suffix.length() != word.length())
      {
        out.append(suffix).append(", ");
      }
      else
      {
        out.append(suffix);
      }
    }

    out.append("}");

    return out.toString();
  }
}

Arquivo Application.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.lfa.tutorial01.topico07.exercicio03;

import java.util.Scanner;

/**
 * Classe responsavel pela execucao da classe Suffix
 */
public class Application
{
  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Forneça a palavra desejada: ");
    String word = scanner.nextLine().trim();

    Suffix suffix = new Suffix(word);

    System.out.println("Observação: \"&\" representa a palavra vazia");
    System.out.print("Sufixos: ");
    System.out.println(suffix);

    scanner.close();
  }
}

Saída da Implementação na Linguagem de Programação Java

Forneça a palavra desejada: ybadoo
Sufixos: {&, o, oo, doo, adoo, badoo, ybadoo}
Observação: & representa a palavra vazia