CF 494B 【Obsessive String】

18 篇文章 0 订阅
2 篇文章 0 订阅

很有趣的一道题

这道题提议很难懂,其实就是让你求合法的集合数目。合法的集合定义为:

1、集合中的所有串都是s的子串,且互不重叠 2、集合中的所有串都含有子串t。

看到网上很多题解说要用kmp,但我就不用...

因为仅需进行一个字符串匹配,而hash是很好写的匹配啊

而且kmp的next指针在dp中并没有起到作用。

说一下主体思路吧:

设两个字符串为s,t,长度分别为l1,l2

首先我们在原串中查找所有的位置i,使s中以i为结尾的子串与t匹配

对于所有的位置i,标记flag[i]=1;

然后我们进行dp

设dp[i]表示以选取的所有集合中集合的最后一个元素的结尾均为i,开头为j(j不体现在状态中,1<=j<=i-l2+1)的所有方案数

那么答案就是∑(i=1~l1)dp[i]

接下来我们考虑转移

首先,对于某一位置,如果flag[i]=0,我们有:

dp[i]=dp[i-1]

原因:如果到这一位置没有匹配上,那么说明这个位置只能被包含在前一个状态中。

那么,如果flag[i]=1,怎么办?

我们考虑集合中元素的个数:

如果只有一个元素,那么由于flag[i]=1,说明i-l2+1~i与t是可以完全匹配的,所以从1到i-l2+1都可以作为这个元素的起点,所以方案数为i-l2+1

如果有两个以上元素,那么最后一个元素的起点就可以是2~i-l2+1

那么我们设这个起点是k

于是上一个元素的终点就可以是1~k-1

所以如果起点是k,总方案数就是∑(i=1~k-1)dp[i]

那这个东西就可以用一个前缀和s来维护

而由于终点可以是2~i-l2+1,所以一共的总方案数就是:

∑(i=2-i-l2+1)s[i-1]

也就是:

∑(i=1-i-l2)s[i]

发现这也是一个前缀和的形式,于是我们把s维护成一个前缀和ss

结合以上两个分析,得到转移为:

dp[i]=i-l2+1+ss[i-l2]

边递推边维护即可。

 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#define seed 13131
#define ull unsigned long long
#define mode 1000000007
#define ll long long
using namespace std;
ll dp[100005];
ll s1[100005];
ll s2[100005];
char s[100005];
char t[100005];
ull has,has1[100005];
ull v;
bool flag[100005];
int main()
{
    scanf("%s%s",s+1,t+1);
    v=1;
    int l1=strlen(s+1),l2=strlen(t+1);
    for(int i=1;i<=l1;i++)
    {
        has1[i]=has1[i-1]*seed+s[i]-'a'+1;
    }
    v=1;
    for(int i=1;i<=l2;i++)
    {
        has=has*seed+t[i]-'a'+1;
        v*=seed;
    }
    for(int i=l2;i<=l1;i++)
    {
        int st=i-l2;
        ull hast=has1[i]-has1[st]*v;
        if(hast==has)
        {
            flag[i]=1;
        }
    }
    for(int i=1;i<=l1;i++)
    {
        if(!flag[i])
        {
            dp[i]=dp[i-1];
        }else
        {
            dp[i]=((i-l2+1)+s2[i-l2])%mode;
        }
        s1[i]=(s1[i-1]+dp[i])%mode;
        s2[i]=(s2[i-1]+s1[i])%mode;
    }
    printf("%lld\n",s1[l1]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,没有找到明星专家系统的相关资料。但是,如果你想要实现一个基于Python的专家系统,可以使用Python的专家系统工具包pyke。pyke是一个Python库,用于开发和运行专家系统。它使用Prolog语法来表示知识,并使用Python来编写规则和操作。以下是一个简单的示例,演示如何使用pyke实现一个基本的专家系统: ```python # 安装pyke !pip install pyke # 导入pyke import pyke # 创建一个专家系统引擎 engine = pyke.engine() # 定义规则 engine.prove_1_goal(''' symptom("cough") symptom("fever") symptom("headache") symptom("sore throat") symptom("runny nose") symptom("muscle aches") symptom("fatigue") symptom("vomiting") symptom("diarrhea") symptom("rash") symptom("red eyes") symptom("hiccups") symptom("sneezing") symptom("swollen glands") symptom("loss of appetite") symptom("nausea") symptom("chills") symptom("chest pain") symptom("shortness of breath") symptom("wheezing") symptom("coughing up blood") symptom("rapid heartbeat") symptom("dizziness") symptom("confusion") symptom("seizures") symptom("numbness") symptom("tingling") symptom("weakness") symptom("paralysis") symptom("vision problems") symptom("hearing problems") symptom("speech problems") symptom("balance problems") symptom("memory problems") symptom("personality changes") symptom("depression") symptom("anxiety") symptom("hallucinations") symptom("delusions") symptom("suicidal thoughts") symptom("homicidal thoughts") symptom("violent behavior") symptom("sexual dysfunction") symptom("menstrual problems") symptom("impotence") symptom("infertility") symptom("pregnancy") symptom("miscarriage") symptom("stillbirth") symptom("birth defects") symptom("developmental delays") symptom("learning disabilities") symptom("behavioral problems") symptom("autism") symptom("down syndrome") symptom("cerebral palsy") symptom("muscular dystrophy") symptom("multiple sclerosis") symptom("parkinson's disease") symptom("alzheimer's disease") symptom("dementia") symptom("schizophrenia") symptom("bipolar disorder") symptom("major depression") symptom("anorexia nervosa") symptom("bulimia nervosa") symptom("binge eating disorder") symptom("substance abuse") symptom("alcoholism") symptom("drug addiction") symptom("gambling addiction") symptom("internet addiction") symptom("shopping addiction") symptom("hoarding disorder") symptom("obsessive-compulsive disorder") symptom("post-traumatic stress disorder") symptom("acute stress disorder") symptom("generalized anxiety disorder") symptom("panic disorder") symptom("social anxiety disorder") symptom("specific phobias") symptom("agoraphobia") symptom("claustrophobia") symptom("acrophobia") symptom("arachnophobia") symptom("ophidiophobia") symptom("emphysema") symptom("asthma") symptom("bronchitis") symptom("pneumonia") symptom("tuberculosis") symptom("lung cancer") symptom("heart disease") symptom("high blood pressure") symptom("low blood pressure") symptom("heart attack") symptom("stroke") symptom("aneurysm") symptom("peripheral artery disease") symptom("deep vein thrombosis") symptom("pulmonary embolism") symptom("anemia") symptom("leukemia") symptom("lymphoma") symptom("multiple myeloma") symptom("hemophilia") symptom("sickle cell anemia") symptom("thalassemia") symptom("hemochromatosis") symptom("diabetes") symptom("hypoglycemia") symptom("hyperthyroidism") symptom("hypothyroidism") symptom("addison's disease") symptom("cushing's syndrome") symptom("polycystic ovary syndrome") symptom("endometriosis") symptom("fibroids") symptom("ovarian cancer") symptom("prostate cancer") symptom("testicular cancer") symptom("breast cancer") symptom("cervical cancer") symptom("ovarian cysts") symptom("uterine prolapse") symptom("urinary incontinence") symptom("urinary tract infections") symptom("kidney stones") symptom("bladder cancer") symptom("kidney cancer") symptom("liver cancer") symptom("pancreatic cancer") symptom("gallbladder disease") symptom("ulcers") symptom("gastroesophageal reflux disease") symptom("irritable bowel syndrome") symptom("inflammatory bowel disease") symptom("diverticulitis") symptom("colon cancer") symptom("hemorrhoids") symptom("anal fissures") symptom("anal cancer") symptom("skin cancer") symptom("psoriasis") symptom("eczema") symptom("acne") symptom("rosacea") symptom("warts") symptom("cold sores") symptom("shingles") symptom("herpes") symptom("hiv/aids") symptom("hepatitis") symptom("malaria") symptom("dengue fever") symptom("zika virus") symptom("ebola virus") symptom("influenza") symptom("measles") symptom("mumps") symptom("rubella") symptom("chickenpox") symptom("polio") symptom("rabies") symptom("tetanus") symptom("meningitis") symptom("encephalitis") symptom("west nile virus") symptom("lyme disease") symptom("rocky mountain spotted fever") symptom("zoonotic diseases") symptom("food poisoning") symptom("botulism") symptom("salmonella") symptom("e. coli") symptom("listeria") symptom("campylobacter") symptom("giardia") symptom("cryptosporidium") symptom("toxoplasmosis") symptom("tapeworms") symptom("roundworms") symptom("hookworms") symptom("pinworms") symptom("scabies") symptom("lice") symptom("bed bugs") symptom("fleas") symptom("ticks") symptom("mosquitoes") symptom("bees") symptom("wasps") symptom("spiders") symptom("snakes") symptom("jellyfish") symptom("ticks") symptom("mosquitoes") symptom("bees") symptom("wasps") symptom("spiders") symptom("snakes") symptom("jellyfish") symptom("sunburn") symptom("heat stroke") symptom("hypothermia") symptom("frostbite") symptom("dehydration") symptom("heat exhaustion") symptom("altitude sickness") symptom("motion sickness") symptom("jet lag") symptom("insomnia") symptom("sleep apnea") symptom("narcolepsy") symptom("restless legs syndrome") symptom("bruxism") symptom("tmj disorder") symptom("migraines") symptom("tension headaches") symptom("cluster headaches") symptom("sinus headaches") symptom("glaucoma") symptom("cataracts") symptom("macular degeneration") symptom("retinal detachment") symptom("conjunctivitis") symptom("dry eye syndrome") symptom("corneal abrasion") symptom("ear infections") symptom("tinnitus") symptom("meniere's disease") symptom("hearing loss") symptom("nosebleeds") symptom("deviated septum") symptom("sinusitis") symptom("tonsillitis") symptom("strep throat") symptom("laryngitis") symptom("pharyngitis") symptom("gastroenteritis") symptom("appendicitis") symptom("gallstones") symptom("kidney stones") symptom("pancreatitis") symptom("diverticulitis") symptom("ulcerative colitis") symptom("crohn's disease") symptom("hemorrhoids") symptom("anal fissures") symptom("hernias") symptom("varicose veins") symptom("lymphedema") symptom("carpal tunnel syndrome") symptom("tennis elbow") symptom("golfer's elbow") symptom("rotator cuff injuries") symptom("whiplash") symptom("back pain") symptom("sciatica") symptom("herniated discs") symptom("spinal stenosis") symptom("scoliosis") symptom("osteoporosis") symptom("arthritis") symptom("bursitis") symptom("tendinitis") symptom("plantar fasciitis") symptom("sprains") symptom("strains") symptom("fractures") symptom("dislocations") symptom("concussions") symptom("traumatic brain injuries") symptom("spinal cord injuries") symptom("amputations") symptom("burns") symptom("cuts") symptom("bruises") symptom("abrasions") symptom("bites") symptom("stings") symptom("poisoning") symptom("allergic reactions") symptom("anaphylaxis") symptom("fainting") symptom("seizures") symptom("stroke") symptom("heart attack") symptom("shock") symptom("hypoglycemia") symptom("hyperglycemia") symptom("dehydration") symptom("heat stroke") symptom("hypothermia") symptom("drowning") symptom("electric shock") symptom("radiation sickness") symptom("carbon monoxide poisoning") symptom("lead poisoning") symptom("mercury poisoning") symptom("arsenic poisoning") symptom("asbestos exposure") symptom("silicosis") symptom("black lung disease") symptom("mesothelioma") symptom("noise-induced hearing loss") symptom("vibration white finger") symptom("repetitive strain injury") symptom("computer vision syndrome") symptom("sick building syndrome") symptom("multiple chemical sensitivity") symptom("electromagnetic hypersensitivity") symptom("burnout") symptom("compassion fatigue") symptom("vicarious trauma") symptom("secondary traumatic stress disorder") symptom("workplace bullying") symptom("sexual harassment") symptom("discrimination") symptom("workplace violence") symptom("domestic violence") symptom("child abuse") symptom("elder abuse") symptom("sexual assault") symptom("rape") symptom("human trafficking") symptom("terrorism") symptom("war") symptom("natural disasters") symptom("accidents") symptom("crime") symptom("death") symptom("grief") symptom("loss") symptom("loneliness") symptom("isolation") symptom("homesickness") symptom("culture shock") symptom("identity crisis") symptom("existential crisis") symptom("midlife crisis") symptom("quarter-life crisis") symptom("retirement") symptom("empty nest syndrome") symptom("relationship problems") symptom("divorce") symptom("breakup") symptom("infidelity") symptom("domestic violence") symptom("childhood trauma") symptom("abandonment") symptom("neglect") symptom("physical abuse") symptom("sexual abuse") symptom("emotional abuse") symptom("verbal abuse") symptom("bullying") symptom("peer pressure") symptom("substance abuse") symptom("addiction") symptom("gambling addiction") symptom("internet addiction") symptom("shopping addiction") symptom("hoarding disorder") symptom("obsessive-compulsive disorder") symptom("post-traumatic stress disorder") symptom("acute stress disorder") symptom("generalized anxiety disorder") symptom("panic disorder") symptom("social anxiety disorder") symptom("specific phobias") symptom("agoraphobia") symptom("claustrophobia") symptom("acrophobia") symptom("arachnophobia") symptom("ophidiophobia") symptom("depression") symptom("major depression") symptom("bipolar disorder") symptom("schizophrenia") symptom("dementia") symptom("alzheimer's disease") symptom("parkinson's disease") symptom("multiple sclerosis") symptom("cancer") symptom("heart disease") symptom("diabetes") symptom("asthma") symptom("arthritis") symptom("osteoporosis") symptom("back pain") symptom("migraines") symptom("anxiety") symptom("stress") symptom("insomnia") symptom("obesity") symptom("smoking") symptom("alcoholism") symptom("drug addiction") symptom("gambling addiction") symptom("internet addiction") symptom("shopping addiction") symptom("hoarding disorder") symptom("obsessive-compulsive disorder") symptom("post-traumatic stress disorder") symptom("acute stress disorder") symptom("generalized anxiety disorder") symptom("panic disorder") symptom("social anxiety disorder") symptom("specific phobias") symptom("agoraphobia") symptom("claustrophobia") symptom("acrophobia") symptom("arachnophobia") symptom("ophidiophobia") symptom("depression") symptom("major

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值