http://acm.nyist.net/JudgeOnline/problem.php?pid=5
我的答案:
java
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int count;
String a, b;
count=cin.nextInt();
while (count>0) {
a = cin.next();
b = cin.next();
int result=0;
if(a.length()>b.length())
{
System.out.println(result);
}else{
int index=b.indexOf(a);
while(index!=-1)
{
result++;
int fromIndex=index;
index=b.indexOf(a,fromIndex+1);
}
System.out.println(result);
}
count--;
}
cin.close();
}
}
c++
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int count;
string a,b;
cin>>count;
while (count--) {
cin>>a;
cin>>b;
int result=0;
if(a.length()>b.length())
{
cout<<result<<endl;
}else{
int index=b.find(a);
while(index!=-1)
{
result++;
int fromIndex=index;
index=b.find(a,fromIndex+1);
}
cout<<result<<endl;
}
}
return 0;
}
标准答案:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
int n;
cin>>n;
while(n--)
{
cin>>s1>>s2;
unsigned int m=s2.find(s1,0);
int num=0;
while(m!=string::npos)
{
num++;
m=s2.find(s1,m+1);
}
cout<<num<<endl;
}
}