hihoCoder挑战赛17 A String Problem I 题库 1260

题意

中文题:http://hihocoder.com/problemset/problem/1260

思路

  • 建一个字典树,暴力dfs求解每个提问

实现

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <assert.h>
#include <string>
#include <set>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 100005;
const int maxm = 10004;
int n,m;

struct Node{
    char ch;
    bool mark;
}node[maxn];
vector<int> g[maxn];
char str[maxn];
int num = 1;

void add(int len){
    int now = 0;
    for (int i=0;i<len;i++){
        int flag = 0;
        for (int j=0;j<g[now].size();j++){
            if (node[g[now][j]].ch == str[i]){
                flag = 1;
                now = g[now][j];
                break;
            }
        }
        if (flag == 1)
            continue;
        node[num].ch = str[i];
        node[num].mark = 0;
        g[now].pb(num);
        now = num;
        num++;
    }
    node[now].mark = 1;
}

int dfs(int u,int dep,int mark){

    if (dep == strlen(str) + 1 ){
        if (node[u].mark == 1)
            return 1;
        else
            return 0;
    }
    int ret = 0;
    for (int i=0;i<g[u].size();i++){
        if (node[g[u][i]].ch != str[dep-mark]){
            if (mark == 1){
                continue;
            }
            else{
                ret += dfs(g[u][i],dep+1,mark+1);
            }
        }
        else{
            ret += dfs(g[u][i],dep+1,mark);
        }
    }
    return ret;

}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;i++){
        scanf("%s",str);
        add(strlen(str));
    }
    for (int i=0;i<m;i++){
        scanf("%s",str);
        printf("%d\n",dfs(0,0,0));
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值