俩代码 未完成

#include <cstring>
#include <cstdio>
#define N 500010
struct node
{
    int cnt;
    node * next[27],* fail;
}*Q[N],*root;
int T;
node * create()
{
    node * rt=new node;
    rt->cnt=0;
    rt->fail=0;
    memset(rt->next,0,sizeof(rt->next));
    return rt;
}
void ins(char *a)
{
    node * p=root;
    char *q=a;
    while(*q)
    {
        int id=*q-‘a‘+1;
        if(p->next[id]==NULL) p->next[id]=create();
        p=p->next[id];
        q++;
    }
    p->cnt++;
}
void build()
{
    int head=0,tail=1;
    Q[tail]=root;
    while(head!=tail)
    {
        node * now=Q[++head];
        node * tmp=NULL;
        for(int i=1;i<=26;i++)
        {
            if(now->next[i]!=NULL)
            {
                if(now==root) now->next[i]->fail=root;
                else
                {
                    tmp=now->fail;
                    while(tmp!=NULL)
                    {
                        if(tmp->next[i]!=NULL)
                        {
                            now->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp==NULL)
                        now->next[i]->fail=root;
                }
                Q[++tail]=now->next[i];
            }
        }
    }
}
int query(char *a)
{
    int ret=0;
    char *q=a;
    node *p=root;
    while(*q)
    {
        int id=*q-‘a‘+1;
        while(p->next[id]==NULL&&p!=root) p=p->fail;
        p=p->next[id];
        if(p==NULL) p=root;
        node *tmp=p;
        while(tmp!=root&&tmp->cnt!=-1)
        {
            ret+=tmp->cnt;
            tmp->cnt=-1;
            tmp=tmp->fail;
        }
        ++q;
    }
    return ret;
}
int main()
{
    scanf("%d",&T);
    for(int n;T--;)
    {
        root=create();
        char str[55];
        scanf("%d",&n);
        for(;n--;)
        {
            scanf("%s",str);
            ins(str);
        }
        build();
        char key[1000005];
        scanf("%s",key);
        printf("%d\n",query(key));
    }
    return 0;
}

2222

#include <cstring>
#include <cstdio>
#define N 100001
struct node
{
    int cnt;
    node * next[53],* fail;
}*root,*Q[N];
node * create()
{
    node * rt=new node;
    rt->fail=NULL;
    rt->cnt=0;
    memset(rt->next,0,sizeof(rt->next));
    return rt;
}
int n;
int f(char c)
{
    if(c<=‘Z‘) return c-‘A‘;
    else return c-‘a‘+26;
}
void ins(char *a)
{
    char *q=a;
    node *p=root;
    while(*q)
    {
        int id=f(*q);
        if(p->next[id]==NULL) p->next[id]=create();
        p=p->next[id];
        ++q;
    }
    p->cnt++;
}
void build()
{
    int head=0,tail=1;
    Q[tail]=root;
    while(head!=tail)
    {
        node * now=Q[++head];
        node * tmp=NULL;
        for(int i=0;i<=52;i++)
        {
            if(now->next[i]!=NULL)
            {
                if(now==root) now->next[i]->fail=root;
                else
                {
                    tmp=now->fail;
                    while(tmp!=NULL)
                    {
                        if(tmp->next[i]!=NULL)
                        {
                            now->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp==NULL)
                        now->next[i]->fail=root;
                }
                Q[++tail]=now->next[i];
            }
        }
    }
}
int query(char *a)
{
    char *q=a;
    node * p=root;
    int ret=0;
    while(*q)
    {
        int id=f(*q);
        while(p->next[id]==NULL&&p!=root) p=p->fail;
        p=p->next[id];
        if(p==NULL) p=root;
        node * tmp=p;
        while(tmp!=root&&tmp->cnt!=-1)
        {
            ret+=tmp->cnt;
            tmp->cnt=-1;
            tmp=tmp->fail;
        }
        ++q;
    }
    return ret;
}
int main()
{
    root=create();
    scanf("%d",&n);
    char str[1000005];
    for(;n--;)
    {
        scanf("%s",str);
        ins(str);
    }
    build();
    scanf("%s",str);
    printf("%d\n",query(str));
    return 0;
}

lg

时间: 2024-07-29 17:36:20

俩代码 未完成的相关文章

学校管理系统代码未完成版

代码还没完成,遇到了瓶颈,请看的朋友留言给我,给点建议,完善一下 1 import datetime 2 3 class School(object): 4 '''总部学校类''' 5 def __init__(self,name,addr,website): 6 self.name = name 7 self.addr = addr 8 self.branches = {} #下属分校 9 self.website = website 10 self.balance = 0 11 print(

130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, th

PHP 代码规范、流程规范、git规范

代码规范.git规范.teambition规范.yii规范 1. 命名规范 (1).变量命名规范 1.变量使用驼峰命名法 禁止使用拼音或者拼音加数字 2.变量也应具有描述性,杜绝一切拼音.或拼音英文混杂的命名方式 3.变量包数字.字母和下划线字符,不允许使用其他字符,变量命名最好使用项目 中有据可查的英文缩写方式, 尽可以要使用一目了然容易理解的形式: 4.变量以字母开头,如果变量包多个单词,首字母小写,当包多个单词时,后面 的每个单词的首字母大写. 例如 :$itSports 5.变量使用有效

Pascal小游戏 贪吃蛇

一段未完成的Pascal贪吃蛇 说这段代码未完成其实是没有源代码格式化,FP中一行最多只有255字符宽. uses crt; const screenwidth=50; screenheight=24; wallchar='#'; snakechar='*'; ; type point=record x,y:integer; end; var snake:array [0..500] of point; map:array [0..screenwidth,0..screenheight] of

【设计模式之禅 第2版】读书笔记

自己练习的源码地址:https://git.oschina.net/snnhoo/DesignPattern.git  欢迎推送 第一章 单一职责原则 简称SRP:Single Responsibility Principle 定义:应该有且仅有一个原因引起类的变更 好处: 类复杂度降低,职责明确 可读性高 可维护性高 变更引起风险低 接口设计要单一,实现要根据情况,方法也适用单一职责 第二章 里氏替换原则 定义:父类能出现的地方就能用子类,且不会产生任何异常,但是反过来则不行,有子类出现的地方

2016年1月25日 《1024伐木累》-小白篇之开发网站,三天!(中篇-2奇怪的IE)-总章节十一

往期回顾:  老王的“先见之明”,解决了困扰耗仔三人的大难题.顺利安装完开发工具,大家投入紧张的工作.航空部领导的突然闯入,IE不兼容,页面错乱,摆在三人面前的形势依然严峻.第一次见这阵仗的耗仔,又会怎么办? 2016-02-01<1024伐木累>-小白篇之开发网站,三天!(中篇-2奇怪的IE) # region 总章节11 IE出了问题,来不及找原因,屋内就涌进一堆领导,你一言我一语,围着三人叽叽喳喳说个不停. 此时的耗仔头都大了,这到底是要干啥,刚开始没几个小时,再这么折腾下去,还怎么工作

Python基础篇(五)

bool用于判断布尔值的结果是True还是False >>> bool("a") True >>> bool(3) True >>> bool("") False >>> bool(0) False Python中的elif类似于Java中的elseif >>> number = (int)(input("input a number: ")) input

HTML5 程序设计 - 使用HTML5 Canvas API

请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方便查看. 学习笔记,纯手工码字,有错别字什么的请指出,觉得好的请点个赞小小的支持下.谢谢亲们. 本篇,我们将探索如何使用HTML5和Canvas API.Canvas API很酷,可以通过它来动态生成和展示图形.图表.图像以及动画. 本篇将使用渲染API(Rendering API)的基本功能来创建

## 20155336 2016-2017-2《JAVA程序设计》第八周学习总结

20155336 2016-2017-2<JAVA程序设计>第八周学习总结 教材学习内容总结 第14章 NIO与NIO2 NIO简介 NIO使用频道来衔接数据结点,在处理数据时,NIO可以让你设定缓冲区容量,在缓冲区中对感兴趣的数据区块进行标记,像是标记读取位 置.数据有效位置,对于这些区块标记,提供了Clear().rewind().flip().compact()等高级操作. NIO2简介 NIO2文件系统API提供一组标准接口与类,应用程序开发者只要基于这些标准接口与类进行文件系统操作,