输入包含多组数据。
每组数据包含两个字符串s,t,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。
对应每组输入,输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就输出0,每个结果占一行。
abcde a3 aaaaaa aa
0 3
//贪心算法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
String s = in.next();
String t = in.next();
int ans = cut(s,t);
System.out.println(ans);
}
}
public static int cut(String s, String t) {
int indexS = s.indexOf(t);
if(indexS == -1) {
return 0;
}
return 1 + cut(s.substring(indexS + t.length()),t);
}
} #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>
#define INF 1000000
using namespace std;
int main(int argc, char** argv)
{
//freopen("in.txt", "r", stdin);
string s, t;
while (cin >> s >> t)
{
int res = 0;
size_t pos = 0;
while ((pos = s.find(t,pos)) != string::npos)
{
pos += t.size();
++res;
}
cout << res << endl;
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1005;
int Next[maxn];
int n,m;
char s[maxn],t[maxn];
void init(int num){
Next[0]=-1;
Next[1]=0;
int pos=2,cn=0;
while(pos<m){
if(t[pos-1]==s[cn]){
Next[pos++]=++cn;
}
else if(cn>0){
cn=Next[cn];
}
else{
Next[pos++]=0;
}
}
}
void print(int num){
for(int i=0;i<num;i++){
printf("%d ",Next[i]);
}
printf("\n");
}
void solve(){
int ans=0;
int i=0,j=0;
while(i<n){
if(s[i]==t[j]){
i++;
j++;
if(j==m){
j=0;
ans++;
}
}
else if(Next[j]>-1){
j=Next[j];
}
else{
i++;
}
}
printf("%d\n",ans);
}
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%s%s",s,t)>0){
n=strlen(s),m=strlen(t);
init(m);
//print(m);
solve();
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
string s;
string t;
int nxt[1005];
void cal_next() {
nxt[0] = -1;
int n = t.size();
int i=1,j=0;
while (i < n) {
if (j == -1 || t[i] == t[j]) {
j++;
i++;
if (t[i] == t[j]) {
nxt[i] = nxt[j];
}
else {
nxt[i] = j;
}
}
else {
j = nxt[j];
}
}
}
int main() {
int i, j;
while (cin >> s >> t) {
int count = 0;
cal_next();
int i, j;
int n = s.size();
int m = t.size();
i = j = 0;
while (i < n) {
if (j== -1 || s[i] == t[j]) {
i++;
j++;
if (j == m) {
j = 0;
count++;
}
}
else {
j = nxt[j];
}
}
cout << count << endl;
}
return 0;
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
String s=scanner.next();
String t=scanner.next();
int count=0;
while(s.contains(t)){
s=s.replaceFirst(t,"");
count++;
}
System.out.println(count);
}
}
} #include <iostream>
#include <string>
using namespace std;
int main(){
string str, s;
int count = 0;
while(cin >> str >> s){
count = 0;
size_t pos = 0;
//while((pos = str.find(s)) != string::npos){
// str.erase(pos, pos + s.size());
// ++count;
//}
while((pos = str.find(s, pos)) != string::npos){
pos += s.size();
++count;
}
cout << count << endl;
}
return 0;
}
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String cotton = in.next();
String small = in.next();
int count = 0;
for (int i = 0; i cotton.length(); ) {
int index = i;
for (int j = 0; j small.length(); j++) {
if (index cotton.length() && cotton.charAt(index) == small.charAt(j)) {
index++;
} else {
break;
}
}
if (index - i == small.length()) {
count++;
i += small.length();
} else {
i++;
}
}
System.out.println(count);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.next();
String t = in.next();
System.out.println(sub(str,t));
}
}
public static int sub(String str,String t){
int count = 0;
int i = str.indexOf(t);
while(i != -1){
count++;
i = str.indexOf(t, i + t.length());
}
return count;
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str1 = scanner.next();
String str2 = scanner.next();
int length2 = str2.length();
int length1 = str1.length();
int count = 0;
for (int i = 0; i < length1; i++) {
//若第一个字母相同则继续比较下去
if (str1.charAt(i) == str2.charAt(0)) {
boolean flag = true;
int j = 0;
for (j = 0; j < length2; j++) {
if ( i + j < length1&& str2.charAt(j) != str1.charAt(j + i) ) {
flag = false;
break;
}
}
if ( i + j < length1+1 && flag) {
count++;
i += length2 - 1;
}
} else {
//若第一个字母都不同就跳下一个字母
continue;
}
}
System.out.println(count);
}
}
} #include <bits/stdc++.h>
using namespace std;
#define int long long
int ne[1005];
void qw(string s) {
int l = s.length();
int i = 0, j = -1;
ne[0] = -1;
while (i < l) {
if (j == -1 || s[i] == s[j]) {
i++;
j++;
ne[i] = j;
} else j=ne[j];
}
}
int as(string s, string t) {
int l = s.length();
int le = t.length();
int j = 0, i = 0;
int cnt = 0;
while (i < l && j < le) {
if (j == -1 || s[i] == t[j]) {
i++, j++;
} else {
j = ne[j];
}if (j == le) {
cnt++;
j = 0;
}
}
return cnt;
}
signed main() {
string S, T;
while (cin >> S && S[0] != '#') {
cin >> T;
qw(T);
cout << as(S, T)<<endl;
}
return 0;
} KMP in C
#include<stdio.h>
#include<string.h>
char s1[1001],s2[1001];
int p[1001],slen,tlen,cnt;
void init_p(char t[]){
int j=0,i=2;
for(i;i<=tlen;i++){
while(j&&t[j+1]!=t[i])j=p[j];
if(t[j+1]==t[i])j++;
p[i]=j;
}
}
void kmp(char s[],char t[]){
int j=0,i=1;
for(i;i<=slen;i++){
while(j>0&&t[j+1]!=s[i])j=p[j];
if(t[j+1]==s[i])j++;
if(j==tlen){
j=0;
cnt++;
}
}
}
main(){
while(scanf("%s",s1+1)!=EOF){
slen=strlen(s1+1);
if(slen==1&&s1[1]=='#')break;
memset(p,0,sizeof(p));
cnt=0;
scanf("%s",s2+1);
tlen=strlen(s2+1);
init_p(s2);
kmp(s1,s2);
printf("%d\n",cnt);
}
}
// 直接利用string中的find逐个查找
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, t;
while (cin >> s >> t)
{
int count = 0;
auto pos = s.find(t, 0);
while(pos != string::npos)
{
count++;
// 下一次开始查找的位置是pos + t的大小
pos = s.find(t, pos + t.size());
}
cout << count << endl;
}
} import java.util.Scanner;
/*
* 剪花布条
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next(); //花布条
String str2 = sc.next(); //小饰条
int fast = 0, slow = 0; //快慢指针
int count = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.substring(slow, fast + 1).contains(str2)) {
count++;
slow = fast +
1; //开始新的寻找,往后继续寻找,前面找过的部分不要
fast++;
} else {
fast++;//往后继续寻找
}
}
System.out.println(count);
}
}
} import java.util.*;
import java.io.*;
public class Main{
public static int cut(String str1,String str2){
int i = str1.indexOf(str2);
if(i == -1){ //查找终止条件
return 0;
}
return 1 + cut(str1.substring(i+str2.length()),str2);
}
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str;
while((str = reader.readLine()) != null){
int index = str.indexOf(" ");
String str1 = str.substring(0,index);
String str2 = str.substring(index+1);
int tmp = cut(str1,str2);
System.out.println(tmp);
}
}
} // write your code here
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
String[] ss = s.split(" ");
String s1 = ss[0];
String s2 = ss[1];
int n = s2.length();
int ans = 0;
int i = s1.indexOf(s2);
while (i != -1) {
ans++;
s1 = s1.substring(i + n);
i = s1.indexOf(s2);
}
System.out.println(ans);
}
}
} import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
String ret = in.next();
int count = 0;
while(str.contains(ret)){
count ++;
str = str.replaceFirst(ret," ");
}
System.out.println(count);
}
in.close();
}
}
import java.util.*;
public class Main{
public static int fun(String s,String t){
int i = s.indexOf(t);
if(i == -1){
return 0;
}
return 1 + fun(s.substring(i + t.length()),t);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = sc.next();
String t = sc.next();
int res = fun(s,t);
System.out.println(res);
}
}
} 暴力搜索#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
int len1 = str1.size();
int len2 = str2.size();
unsigned int idx = 0;
int count = 0;
while (idx < len1)
{
idx = str1.find(str2, idx);
// 这里如果没有判断,会出现死循环情况
if (idx >= len1)
break;
idx += len2;
count++;
}
cout << count << endl;
}
return 0;
}