SDUTOJ 2125 數據結構實驗之串二:字符串匹配

數據結構實驗之串二:字符串匹配

Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裏^_^

題目描述

  給定兩個字符串string1和string2,判斷string2是否爲string1的子串。
 

輸入

 輸入包含多組數據,每組測試數據包含兩行,第一行代表string1,第二行代表string2,string1和string2中保證不出現空格。(string1和string2大小不超過100字符)
 

輸出

 對於每組輸入數據,若string2是string1的子串,則輸出"YES",否則輸出"NO"。
 

示例輸入

abc
a
123456
45
abc
ddd

示例輸出

YES
YES
NO
	import java.util.Scanner;
	public class Main{
	
		public static void main(String[] args) 
	{ 
			
		 Scanner sc = new Scanner(System.in);//創建對象以及對象的引用sc
		 
		 while(sc.hasNext())//java中字符串多組輸入的方法
			 
		 {
			String str = sc.next();
			String str1 = sc.next();
			int size = str.indexOf(str1);
			//判斷字符串中是否有匹配的子字符串
			if(size==-1)
			{
				System.out.println("NO");
			}
			else
			{
				System.out.println("YES");
			}
		 }     
		}
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章