首页 > 试题广场 >

substring

[编程题]substring
  • 热度指数:47 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
判断一个string是否是另一个string的子串是一个普遍且重要的问题。现在你也要来解决一个关于子串的小问题。我们现在有一个数量不是很大的string库(0<N <= 500),并且每个string(1<=len <= 20)都不是很长。对给定的string(0<M <= 1000),你要写一个程序算出它是库中多少个string的子串。

输入描述:
有多个case, 每个case第一行是一个正整数N,之后N行每行一个string,代表string库, 每个string由a-z 26个小写字母组成,然后是一个正整数M,之后M行每行一个string代表你所要询问的string。


输出描述:
每个case输出M行,每行一个正整数,代表当前询问的string是库中多少个string的子串
示例1

输入

3
aaa
aaa
baa
2
aa
ba
1
a
1
a

输出

3
1
1
//使用kmp算法暴力求解
#include <bits/stdc++.h>
using namespace std;
void Next(const string &key, vector<int> &next)
{
    next[0] = -1;
    int i = 0, j = -1;
    while (i != key.size()-1)
    {
        if (j == -1 || key[i] == key[j])
        {
            next[++i] = ++j;
        }
        else
        {
            j = next[j];
        }
    }
}
int KMP(const string &word, const string &key)
{
    vector<int> next(key.size(), 0);
    Next(key, next);
    int i = 0, j = 0;
    while (i != word.size() && j != key.size())
    {
        if (j == -1 || word[i] == key[j])
        {
            ++i;
            ++j;
        }
        else
        {
            j = next[j];
        }
    }
    return j == key.size() ? i - j : -1;
}
void CountPreix(const vector<string> &words, const vector<string> &keys, vector<int> &counts)
{
    for (int i = 0; i < keys.size(); ++i)
    {
        for (int j = 0; j < words.size(); ++j)
        {
            if (KMP(words[j], keys[i]) != -1)
            {
                counts[i]++;
            }
        }
    }
}
int main()
{
    int n = 0;
    while (cin >> n)
    {
        vector<string> words(n);
        for (int i = 0; i < n; ++i)
        {
            cin >> words[i];
        }
        int m = 0;
        cin >> m;
        vector<string> keys(m);
        for (int i = 0; i < m; ++i)
        {
            cin >> keys[i];
        }
        vector<int> counts(m, 0);
        CountPreix(words, keys, counts);
        for (int count : counts)
        {
            cout << count << endl;
        }
    }
    
    return 0;
}

发表于 2018-07-11 15:19:37 回复(0)
while True:
    try:
        num = int(input())
        List = []
        for i in range(num):
            List.append(input())
        M = int(input())
        for i in range(M):
            string = input()
            res = 0
            for j in List:
                if string in j:
                    res += 1
            print(res)
    except:
        break

发表于 2019-10-16 13:59:09 回复(0)
/*利用string的find函数*/
#include<iostream>
#include<stdlib.h>
#include<string>
#include<vector>
usingnamespacestd;
 
intmain(){
    intn;
    while(cin>>n) {
        vector<string> lib(n);
        for(inti=0;i<n;i++)
            cin >> lib[i];
        intm;
        cin >>m;
        vector<string> query(m);
        vector<int> num(m);
        for(inti=0;i<m;i++) {
            cin >> query[i];
            num[i]=0;
            for(intj=0;j<n;j++) {
                if(lib[j].find(query[i])!=string::npos) num[i]++;
            }
        }
        for(inti=0;i<m;i++) {
            cout << num[i] <<endl;
        }
    }
    return0;
}
发表于 2019-08-05 16:55:10 回复(0)