剪花布條
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11540 Accepted Submission(s): 7418
Problem Description
一塊花布條,裏面有些圖案,另有一塊直接可用的小飾條,裏面也有一些圖案。對於給定的花布條和小飾條,計算一下能從花布條中儘可能剪出幾塊小飾條來呢?
Input
輸入中含有一些數據,分別是成對出現的花布條和小飾條,其布條都是用可見ASCII字符表示的,可見的ASCII字符有多少個,布條的花紋也有多少種花樣。花紋條和小飾條不會超過1000個字符長。如果遇見#字符,則不再進行工作。
Output
輸出能從花紋布中剪出的最多小飾條個數,如果一塊都沒有,那就老老實實輸出0,每個結果之間應換行。
Sample Input
abcde a3
aaaaaa aa
#
Sample Output
0
3
此題知識點:
熟悉java中的indexof() 和 substring()
indexof() 返回指定子字符串在此字符串中第一次出現處的索引
substring() 返回一個新的字符串,它是此字符串的一個子字符串。
//indeof
String s3 = "22h2345";
String s4 = "h2";
System.out.println(s3.indexOf(s4)); //輸出是2
//substring
String c ="abcd".substring(2,3);// 相當於數學中的:[2,3)
System.out.println(c);//輸出結果是 c
package HDU;
import java.util.Scanner;
public class p2087 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String x=sc.next();
if("#".equals(x)){
return;
}
String y=sc.next();
int cout=0;
int index=x.indexOf(y);
while(index>=0){
cout++;
x=x.substring(index+y.length());
index=x.indexOf(y);
}
System.out.println(cout);
}
}
}