hiho挑战赛17 A String Problem I

1260 : String Problem I

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
我们有一个字符串集合S,其中有N个两两不同的字符串。

还有M个询问,每个询问给出一个字符串w,求有多少S中的字符串可以由w添加恰好一个字母得到。

字母可以添加在包括开头结尾在内的任意位置,比如在”abc”中添加”x”,就可能得到”xabc”, “axbc”, “abxc”, “abcx”.这4种串。

输入
第一行两个数N和M,表示集合S中字符串的数量和询问的数量。

接下来N行,其中第i行给出S中第i个字符串。

接下来M行,其中第i行给出第i个询问串。

所有字符串只由小写字母构成。

数据范围:

N,M<=10000。

S中字符串长度和<=100000。

所有询问中字符串长度和<=100000。

输出
对每个询问输出一个数表示答案。

样例输入
3 3
tourist
petr
rng
toosimple
rg
ptr
样例输出
0
1
1

题意:给出查询的词,问在集合里有多少个字符串可以有查询的词经过在某个位置添加某个字母得到,也就是查询词在某个位置加个字母就能和集合的某字符串相同。

思路:赛后看过别人的做法,有些是哈希过了,还有字典树的,我是用string类直接暴力过了,感觉还是有点不科学的样子,目测刚好卡时间了把……

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
string a[10010];
char b[100105];
int len[10010];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        len[i]=strlen(a[i].c_str());
    }
    while(m--)
    {
        scanf("%s",b);
        int lenb=strlen(b);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            int flag=0;
            const char *c=a[i].c_str();
            if(lenb+1!=len[i])
            {
                continue;
            }
            for(int j=0,k=0;k<len[i];j++,k++)
            {
                if(j>=lenb || c[k]!=b[j])
                {
                    if(flag)
                    {
                        flag=-1;
                        break;
                    }
                    else
                    {
                        flag=1;
                        j--;
                    }
                }
            }
            if(flag!=-1)ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值