Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1140

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1140

Flowers Flourish from France

 

by Ines Kereki Uruguay

Timelimit: 1

Fiona has always loved poetry, and recently she discovered a fascinating poetical form. Tautograms are a special case of alliteration, which is the occurrence of the same letter at the beginning of adjacent words. In particular, a sentence is a tautogram if all of its words start with the same letter.
 
For instance, the following sentences are tautograms:
  • Flowers Flourish from France
  • Sam Simmonds speaks softly
  • Peter pIckEd pePPers
  • truly tautograms triumph
Fiona wants to dazzle her boyfriend with a romantic letter full of this kind of sentences. Please help Fiona to check if each sentence she wrote down is a tautogram or not.

 

Input

 

Each test case is given in a single line that contains a sentence. A sentence consists of a sequence of at most 50 words separated by single spaces. A word is a sequence of at most 20 contiguous uppercase and lowercase letters from the English alphabet. A word contains at least one letter and a sentence contains at least one word.
 
The last test case is followed by a line containing only a single character '*' (asterisk).

 

Output

 

For each test case output a single line containing an uppercase 'Y' if the sentence is a tautogram, or an uppercase 'N' otherwise.

 

 

 

Input Sample Output Sample

Flowers Flourish from France
Sam Simmonds speaks softly
Peter pIckEd pePPers
truly tautograms triumph
this is NOT a tautogram
*

Y
Y
Y
Y
N

 

Code Examples

#1 Code Example with C Programming

Code - C Programming



#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

typedef struct{

	char palavra[21];

} string;

bool isTautograma(string *, unsigned short tam);

int main (void)
{

	unsigned short i;

	while (true)
	{

		char frase[2000] = { 0 }, *palavraTmp;
		string palavras[100];

		scanf(" %[^\n]", frase);

		if (strcmp(frase, "*") == 0)
			break;

		i = 0;
		palavraTmp = strtok(frase, " ");
		strcpy(palavras[i++].palavra, palavraTmp);

		do
		{

			palavraTmp = strtok('\0', " ");

			if (palavraTmp)
				strcpy(palavras[i++].palavra, palavraTmp);

		} while (palavraTmp);

		// Resultado final;
		if (isTautograma(palavras, i))
			printf("Y\n");
		else
			printf("N\n");

	}

}

bool isTautograma(string *palavras, unsigned short tam)
{

	unsigned short i;

	for (i = 1; i  <  tam; i++)
		if (tolower(palavras[i].palavra[0]) != tolower(palavras[i - 1].palavra[0]))
			return false;

	return true;

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Flowers Flourish from France
Sam Simmonds speaks softly
Peter pIckEd pePPers
truly tautograms triumph
this is NOT a tautogram
*

Output

x
+
cmd
Y
Y
Y
Y
N

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <vector>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string s;
    char l, u;
    bool yes;
    while (1)
    {
        getline(cin,s);
        yes=true;
        if (s=="*")
            break;
        if (isupper(s[0]))
        {
            u=s[0];
            l=s[0]-'A'+'a';
        }
        else if (islower(s[0]))
        {
            l=s[0];
            u=s[0]-'a'+'A';
        }
        for (int i = 0; i  <  s.size(); ++i)
        {
            if (s[i]==' ')
            {
                if (s[i+1]!=l && s[i+1]!=u)
                {
                    yes=false;
                    break;
                }
            }
        }
        if (yes)
            cout << "Y" << endl;
        else
            cout << "N" << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Flowers Flourish from France
Sam Simmonds speaks softly
Peter pIckEd pePPers
truly tautograms triumph
this is NOT a tautogram
*

Output

x
+
cmd
Y
Y
Y
Y
N

#3 Code Example with Java Programming

Code - Java Programming


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
   static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) throws IOException {
		String l;
		String[] w;
		while (!(l = in.readLine()).equals("*")) {
			w = l.split("\\s");
			boolean f = true;
			String firstLetter = w[0].substring(0, 1);
			for (int i = 1; i  <  w.length; i++) {
				if (!firstLetter.equalsIgnoreCase(w[i].substring(0, 1))) {
					f = false;
					break;
				}
			}
			out.println(f ? "Y" : "N");
		}
		out.close();
	}
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
Flowers Flourish from France
Sam Simmonds speaks softly
Peter pIckEd pePPers
truly tautograms triumph
this is NOT a tautogram
*

Output

x
+
cmd
Y
Y
Y
Y
N

#4 Code Example with Python Programming

Code - Python Programming


while True:
    entrada = input()
    if entrada == "*":
        break
    if len(set([k[0].lower() for k in entrada.split()])) == 1:
        print("Y")
    else:
        print("N")
Copy The Code & Try With Live Editor

Input

x
+
cmd
Flowers Flourish from France
Sam Simmonds speaks softly
Peter pIckEd pePPers
truly tautograms triumph
this is NOT a tautogram
*

Output

x
+
cmd
Y
Y
Y
Y
N
Advertisements

Demonstration


Previous
#1136 Beecrowd Online Judge Solution 1136 Bingo! Solution in C, C++, Java, Js and Python
Next
#1142 Beecrowd Online Judge Solution 1142 PUM Solution in C++, Java, Js and Python