首页 > 试题广场 >

牛牛的游戏

[编程题]牛牛的游戏
  • 热度指数:1273 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛很喜欢玩接龙游戏,一天他突发奇想,发明了一种叫做“字符串链”的游戏。 这个游戏的规则是这样的,给出3个字符串A,B,C,如果它们满足以下两个条件,那么就可以构成一个“字符串链”:
1.A的最后一个字母和B的第一个字母相同;
2.B的最后一个字母和C的第一个字母相同。
现在牛牛给出了3个字符串A,B,C,希望你能判断这3个字符串能否构成一个“字符串链”,若能则输出“YES”,否则输出“NO”。

输入描述:
一行,3个字符串,每两个字符串之间用一个空格分隔。

1.A,B,C均由小写的英文字母组成;

2.1≤|A|,|B|,|C|≤10,|A|,|B|,|C|分别表示A,B和C的长度。


输出描述:
"YES"或者"NO"(不带引号)。
示例1

输入

b bb b

输出

YES
示例2

输入

a b c

输出

NO
#include<iostream>
#include<string>
using namespacestd;
int main(){
    string A,B,C;
    cin>>A>>B>>C;
    int a=A.length(),b=B.length(),c=C.length();
    if((A[a-1]==B[0])&&(B[b-1]==C[0]))cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

编辑于 2018-08-25 18:40:13 回复(0)
import java.util.Scanner;
public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String [] arr = str.split(" ");
        if(arr[0].charAt(arr[0].length()-1)==arr[1].charAt(0)
           && arr[1].charAt(arr[1].length()-1)==arr[2].charAt(0))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
发表于 2018-08-02 21:33:22 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class test7 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String s1 = in.nextLine();
            String[] ss = s1.split(" ");
            List<String> l = Arrays.asList(ss);
            Boolean[] A ={false};
            judge(l, A, "");
            if(A[0]){
                System.out.println("YES");
            }else{
                System.out.println("NO");
            }
        }
    }

    private static void judge(List<String> l, Boolean[] b, String s) {
        // TODO Auto-generated method stub
        if (l.size() > 0) {
            String ss = new String(s);
            for (int i = 0; i < l.size(); i++) {
                if (s.length() == 0) {
                    ss += l.get(i);
                    List<String> l1 = copyList(l);
                    l1.remove(i);
                    judge(l1, b, ss);
                } else {
                    if (ss.charAt(s.length() - 1) == l.get(i).charAt(0)) {
                        ss += l.get(i);
                        List<String> l1 = copyList(l);
                        l1.remove(i);
                        judge(l1, b, ss);
                    }
                }
            }
        }else{
            b[0]=true;
        }
    }

    private static List<String> copyList(List<String> l) {
        // TODO Auto-generated method stub
        List<String> l1 = new ArrayList<>();
        for (int i = 0; i < l.size(); i++) {
            l1.add(l.get(i));
        }
        return l1;
    }
}
花了点时间,上述代码可用于任意个字符串进行字符串链校验。
发表于 2018-08-16 11:32:25 回复(0)
#include<bits/stdc++.h>
using namespace std;
 
int main(){
    string a,b,c;
    cin >> a >> b >> c;
    int len1 = a.length();
    int len2 = b.length();
    int len3 = c.length();
    if(a[len1-1] == b[0] && b[len2-1] == c[0]){
        cout << "YES";
    }else{
        cout << "NO";
    }
    return 0;
}

编辑于 2018-08-07 11:41:10 回复(0)
all=input()
x=all.split(" ")
a=x[0]
b=x[1]
c=x[2]
ifa[-1]==b[0] andb[-1]==c[0]:
    print("YES")
else:
    print("NO")

发表于 2018-07-28 23:04:29 回复(0)
def func(A,B,C):
    s1 = A[-1]
    s2 = B[0]
    s3 = B[-1]
    s4 = C[0]
    if s1==s2 and s3==s4:
        print('YES')
    else:
        print('NO')
if __name__ == '__main__':
    A,B,C = map(str,input().split())
    func(A,B,C)


发表于 2018-07-26 15:16:46 回复(0)
package test; import java.util.Scanner; public class test2 { public static void main(String[] args) { String s1,s2,s3; Scanner sc = new Scanner(System.in); s1 = sc.next(); s2 = sc.next(); s3 = sc.next(); if( s1.charAt(s1.length()-1) == s2.charAt(0) && s2.charAt(s2.length()-1) == s3.charAt(0)){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
发表于 2018-07-25 15:55:53 回复(0)
#include<bits/stdc++.h>
 
usingnamespacestd;
intmain(){
   charA[10],B[10],C[10];
    
    scanf("%s %s %s",A,B,C);
    intn1=strlen(A);
    intn2=strlen(B);
    intn3=strlen(C);
    if((A[n1-1]==B[0])&&(B[n2-1]==C[0]))
        printf("YES");
    else{printf("NO");}
}

发表于 2018-07-25 11:57:34 回复(0)

抖个机灵

#include <iostream>
#include <string>

using namespace std;
int main() {
    string a, b, c;
    while(cin >> a >> b >> c) {
        if(a.back()==b.front() && b.back() == c.front()) cout << "YES";
        else cout << "NO";
    }
}
发表于 2018-07-25 02:20:02 回复(0)
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
void main()
{
    string A,B,C;
    cin>>A>>B>>C;
    int a=A.length();
    int b=B.length();
    int c=C.length();
    if (A[a-1]==B[0]&&B[b-1]==C[0])
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;
    system("pause");

}

发表于 2018-07-24 22:01:41 回复(0)
#include<iostream>
#include<string.h>
#include<math.h>
 
using namespace std;
 
intmain()
{
    char*a = newchar[10];
    char*b = newchar[10];
    char*c = newchar[10];
 
    cin >> a >> b >> c;
 
    intalen = strlen(a);
    intblen = strlen(b);
    intclen = strlen(c);
 
    if(a[alen - 1] == b[0] && b[blen - 1] == c[0]) cout << "YES"<< endl;
    elsecout << "NO"<< endl;
 
    delete[] a, b, c;
    return0;
}
发表于 2018-07-23 16:04:03 回复(3)
import sys
A,B,C = sys.stdin.readline().strip().split(' ')
if A[-1] == B[0] and B[-1] == C[0]:
    print 'YES'
else:
    print 'NO'
发表于 2018-07-21 23:39:17 回复(0)