ACM___________________________

______________白白の屋
posts - 182, comments - 102, trackbacks - 0, articles - 0

HDOJ HDU 2087 剪花布条 ACM 2087 IN HDU

Posted on 2010-08-07 17:54 MiYu 阅读(518) 评论(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;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理