zoukankan      html  css  js  c++  java
  • 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接:

      http://acm.hdu.edu.cn/showproblem.php?pid=5763

    题目大意

      T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007)。

    题目思路:

      【动态规划】【KMP】

      题目有点绕,看看样例就懂了。其实不用KMP直接用substr就能做。

      首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]表示当前这个s2串用第二种意思解读能多出的解数。

    substr:

     1 //
     2 //by coolxxx
     3 //<bits/stdc++.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<memory.h>
     9 #include<time.h>
    10 #include<stdio.h>
    11 #include<stdlib.h>
    12 #include<string.h>
    13 //#include<stdbool.h>
    14 #include<math.h>
    15 #define min(a,b) ((a)<(b)?(a):(b))
    16 #define max(a,b) ((a)>(b)?(a):(b))
    17 #define abs(a) ((a)>0?(a):(-(a)))
    18 #define lowbit(a) (a&(-a))
    19 #define sqr(a) ((a)*(a))
    20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    21 #define mem(a,b) memset(a,b,sizeof(a))
    22 #define eps (1e-8)
    23 #define J 10000000
    24 #define MAX 0x7f7f7f7f
    25 #define PI 3.1415926535897
    26 #define mod 1000000007
    27 #define N 100004
    28 using namespace std;
    29 typedef long long LL;
    30 int cas,cass;
    31 int n,m,lll,ans;
    32 int f[N];
    33 string s1,s2;
    34 
    35 int main()
    36 {
    37     #ifndef ONLINE_JUDGE
    38     freopen("1.txt","r",stdin);
    39 //    freopen("2.txt","w",stdout);
    40     #endif
    41     int i,j;
    42 //    for(scanf("%d",&cas);cas;cas--)
    43     for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    44 //    while(~scanf("%s",s))
    45 //    while(~scanf("%d",&n))
    46     {
    47         printf("Case #%d: ",cass);
    48         cin>>s1>>s2;
    49         n=s1.length();m=s2.length();
    50         f[0]=1;
    51         for(i=1;i<=n;i++)
    52         {
    53             if(i>=m && s1.substr(i-m,m)==s2)
    54                 f[i]=(f[i-1]+f[i-m])%mod;
    55             else f[i]=f[i-1];
    56         }
    57         printf("%d
    ",f[n]);
    58     }
    59     return 0;
    60 }
    61 /*
    62 //
    63 
    64 //
    65 */
    View Code

    KMP:

     1 //
     2 //by coolxxx
     3 //<bits/stdc++.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<memory.h>
     9 #include<time.h>
    10 #include<stdio.h>
    11 #include<stdlib.h>
    12 #include<string.h>
    13 //#include<stdbool.h>
    14 #include<math.h>
    15 #define min(a,b) ((a)<(b)?(a):(b))
    16 #define max(a,b) ((a)>(b)?(a):(b))
    17 #define abs(a) ((a)>0?(a):(-(a)))
    18 #define lowbit(a) (a&(-a))
    19 #define sqr(a) ((a)*(a))
    20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    21 #define mem(a,b) memset(a,b,sizeof(a))
    22 #define eps (1e-8)
    23 #define J 10000000
    24 #define MAX 0x7f7f7f7f
    25 #define PI 3.1415926535897
    26 #define mod 1000000007
    27 #define N 100004
    28 using namespace std;
    29 typedef long long LL;
    30 int cas,cass;
    31 int n,m,lll,ans;
    32 int Next[N],f[N];
    33 char s1[N],s2[N];
    34 bool mark[N];
    35 void getNext(char s[])
    36 {
    37     int i,j,len;
    38     Next[0]=-1;len=strlen(s);
    39     for(i=0,j=-1;i<len;)
    40     {
    41         if(j==-1 || s[i]==s[j])
    42             Next[++i]=++j;
    43         else j=Next[j];
    44     }
    45 }
    46 void kmp(char s1[],char s2[])
    47 {
    48     int i,j;
    49     mem(mark,0);
    50     getNext(s2);
    51     for(i=0,j=0;i<n;)
    52     {
    53         if(j==-1 || s1[i]==s2[j])i++,j++;
    54         else j=Next[j];
    55         if(j==m)
    56         {
    57             mark[i]=1;
    58             j=Next[j];
    59         }
    60     }
    61 }
    62 int main()
    63 {
    64     #ifndef ONLINE_JUDGE
    65     freopen("1.txt","r",stdin);
    66 //    freopen("2.txt","w",stdout);
    67     #endif
    68     int i,j;
    69 //    for(scanf("%d",&cas);cas;cas--)
    70     for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    71 //    while(~scanf("%s",s))
    72 //    while(~scanf("%d",&n))
    73     {
    74         printf("Case #%d: ",cass);
    75         scanf("%s%s",s1,s2);
    76         n=strlen(s1);m=strlen(s2);
    77         kmp(s1,s2);
    78         f[0]=1;
    79         for(i=1;i<=n;i++)
    80         {
    81             if(mark[i] && i>=m)
    82                 f[i]=(f[i-1]+f[i-m])%mod;
    83             else f[i]=f[i-1];
    84         }
    85         printf("%d
    ",f[n]);
    86     }
    87     return 0;
    88 }
    89 /*
    90 //
    91 
    92 //
    93 */
    View Code
  • 相关阅读:
    梦断代码阅读笔记之一
    市场调研
    站立会议第九天
    站立会议第八天
    站立会议第七天
    站立会议第六天
    站立会议第五天
    团队项目第一阶段冲刺站立会议6(4月23日)
    团队项目第一阶段冲刺站立会议5(4月22日)
    团队项目第一阶段冲刺站立会议4(4月21日)
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5769570.html
Copyright © 2011-2022 走看看