{ }codelper
equality and coercionP0433

Matching torn name tags

Several name tags torn into two pieces were found on the floor of an event hall.
The name of the person you are looking for is given on the first line of input.
The second line gives the number of piece pairs n, and each of the next n lines gives a front piece and a back piece separated by one space.
If the string made by attaching the back piece right after the front piece equals the name, print "yes"; otherwise print "no".
Print one line per pair, in the given order.
The two pieces are never swapped and never overlapped.
Two strings are equal when they have the same length and the same character at every position.
If the joined string is longer or shorter than the name, the answer is "no" even when its beginning matches the name.
The name and the pieces are read separately from input, so inside the program they are different string values.
Compare whether their contents are equal.

Input
The name is 1 to 20 lowercase English letters.
1 ≤ n ≤ 100.
Each piece is 1 to 20 lowercase English letters.
The two pieces on a line are separated by one space.
Output
n lines, each "yes" or "no"
Example 1 · stdin
minji
5
min ji
minj ji
mi nji
min jiz
ji min
stdout
yes
no
yes
no
no
Example 2 · stdin
aa
4
a a
aa a
a aa
aa aa
stdout
yes
no
no
no
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

This problem joins strings and then asks whether the contents are equal.
Joining is + in Python, JavaScript, TypeScript, Java, C# and Craykon, and in C++ + works between std::strings.
C has no string +, so the front piece is put into a 41-slot array (20 + 20 characters and the terminating '\0') with strcpy, and the back piece is attached with strcat.
For comparing, Python ==, JavaScript/TypeScript ===, C# ==, C++ == on std::string and Craykon == all compare the length and every character (C# defines == on string to compare contents).
Java uses joined.equals(name) and C uses strcmp(joined, name) == 0.
Trap one: in Java joined == name asks whether they are the same object.
A string newly built by + at run time is always a different object from the name read from input, so every answer becomes "no", and there is no compile error.
Trap two: in C joined == name compares the addresses of the two arrays.
They are different arrays, so again every answer is "no".
Trap three: in C, comparing only as many characters as the name has, as in strncmp(joined, name, strlen(name)), says "min" + "jiz", which has an extra letter at the end, equals "minji" (the fourth pair of example 1).
Trap four: checking only whether the name starts with the front piece and ends with the back piece (Python name.startswith(front) and name.endswith(back)) without joining never checks the length, so overlapping pieces such as "minj" + "ji" or "aa" + "a" also give "yes" (the second pair of example 1 and the second pair of example 2).

Pythonmain.py · 6 lines
name = input()n = int(input())for _ in range(n):    front, back = input().split()    # 문자열의 ==는 내용(길이와 자리마다의 글자)을 비교한다    print("yes" if front + back == name else "no")
JavaScriptmain.js · 10 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n").map((l) => l.replace(/\r$/, ""));const name = lines[0];const n = Number(lines[1]);const out = [];for (let i = 0; i < n; i++) {  const [front, back] = lines[2 + i].split(" ");  // 문자열끼리의 ===는 내용을 비교한다  out.push(front + back === name ? "yes" : "no");}console.log(out.join("\n"));
TypeScriptmain.ts · 11 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n").map((l) => l.replace(/\r$/, ""));const name: string = lines[0];const n: number = Number(lines[1]);const out: string[] = [];for (let i = 0; i < n; i++) {  const [front, back] = lines[2 + i].split(" ");  // 문자열끼리의 ===는 내용을 비교한다  out.push(front + back === name ? "yes" : "no");}console.log(out.join("\n"));
JavaMain.java · 18 lines
import java.io.BufferedReader;import java.io.InputStreamReader; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String name = br.readLine().trim();        int n = Integer.parseInt(br.readLine().trim());        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            String[] parts = br.readLine().trim().split(" ");            String joined = parts[0] + parts[1];            // String의 ==는 같은 객체인지를 묻는다. 내용은 equals로 비교한다            out.append(joined.equals(name) ? "yes" : "no").append('\n');        }        System.out.print(out);    }}
C#Main.cs · 20 lines
using System;using System.Text; class Program{    static void Main()    {        string name = Console.ReadLine().Trim();        int n = int.Parse(Console.ReadLine().Trim());        var output = new StringBuilder();        for (int i = 0; i < n; i++)        {            string[] parts = Console.ReadLine().Trim().Split(' ');            string joined = parts[0] + parts[1];            // C#의 string ==는 내용을 비교하도록 만들어져 있다            output.AppendLine(joined == name ? "yes" : "no");        }        Console.Write(output);    }}
C++main.cpp · 13 lines
#include <iostream>#include <string> int main() {    std::string name, front, back;    int n;    std::cin >> name >> n;    for (int i = 0; i < n; i++) {        std::cin >> front >> back;        // std::string의 ==는 내용을 비교한다        std::cout << (front + back == name ? "yes" : "no") << "\n";    }}
Cmain.c · 17 lines
#include <stdio.h>#include <string.h> int main(void) {    char name[21], front[21], back[21];    char joined[41]; /* 조각 둘이 20글자씩이면 40글자 + 끝 표시 '\0' */    int n;    scanf("%20s %d", name, &n);    for (int i = 0; i < n; i++) {        scanf("%20s %20s", front, back);        strcpy(joined, front);        strcat(joined, back);        /* 배열끼리의 ==는 주소를 비교한다. 글자는 strcmp로 비교하고, 같으면 0이 나온다 */        puts(strcmp(joined, name) == 0 ? "yes" : "no");    }    return 0;}
Craykonmain.cray · 15 lines
이름 = Console.ReadLine().Trim();n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){    조각 = Console.ReadLine().Trim().Split(" ");    // 문자열끼리의 ==는 내용을 비교한다    if (조각[0] + 조각[1] == 이름)    {        Console.WriteLine("yes");    }    else    {        Console.WriteLine("no");    }}