Posted on 2010-08-07 17:54
MiYu 閱讀(542)
評論(0) 編輯 收藏 引用 所屬分類:
ACM ( 串 ) 、
ACM ( 水題 )
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=2087
題目描述:
Problem Description
一塊花布條,里面有些圖案,另有一塊直接可用的小飾條,里面也有一些圖案。對于給定的花布條和小飾條,計算一下能從花布條中盡可能剪出幾塊小飾條來呢?
Input
輸入中含有一些數據,分別是成對出現的花布條和小飾條,其布條都是用可見ASCII字符表示的,可見的ASCII字符有多少個,布條的花紋也有多少種花樣。花紋條和小飾條不會超過1000個字符長。如果遇見#字符,則不再進行工作。
Output
輸出能從花紋布中剪出的最多小飾條個數,如果一塊都沒有,那就老老實實輸出0,每個結果之間應換行。
Sample Input
abcde a3
aaaaaa aa
#
Sample Output
0
3
水題, 直接使用 C語言的 strstr 或 C++ 的 string ::find() 可以直接求出
C 代碼 :
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
#include <stdio.h>
#include <string.h>
int main(void)
{
int len, c;
char *p;
char a[1001], b[1001];
while (scanf("%s", a), a[0] != '#')
{
scanf("%s", b);
len = strlen(b);
for (c = 0, p = a; p = strstr(p, b); c++,p += len);
printf("%d\n", c);
}
return 0;
}
C++ 代碼 :
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string word,str;
while ( cin >> word , word != "#" )
{
cin >> str;
int nCount = 0;
int len = str.size ();
int pos;
while ( ( pos = word.find ( str ) ) != string::npos )
{
nCount ++;
word = word.substr ( pos + len );
}
cout << nCount << endl;
}
return 0;
}