zoukankan      html  css  js  c++  java
  • P2246 SAC#1

    P2246 SAC#1 - Hello World(升级版)
    典型的字符串dp
    f[i][j]表示a串匹配到i,b串匹配到j的方案数。
    if(a[i]==b[j])f[i][j]=f[i-1][j-1]+f[i-1][j];
    if(a[i]!=b[j])f[i][j]=f[i-1][j];
    显然可以用滚动数组优化空间
    if(a[i]==b[j])f[j]+=f[j-1];
    否则不用管。
    什么?Why?
    f[j-1]不是在f[j]之前更新了吗?
    唉,不要惯性思维啊,倒着匹配不就完了,是不?

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<ctime>
     7 #include<cstring>
     8 #include<map>
     9 #define mod 1000000007
    10 #define For(i,a,b) for(register long long i=a;i<=b;i++)
    11 #define p(a) putchar(a)
    12 #define g() getchar()
    13 //by war
    14 //2017.10.23
    15 using namespace std;
    16 char a[500010],b[20]="@helloworld";
    17 long long cnt;
    18 long long f[20];
    19 void in(long long &x)
    20 {
    21     long long y=1;
    22     char c=g();x=0;
    23     while(c<'0'||c>'9')
    24     {
    25     if(c=='-')
    26     y=-1;
    27     c=g();
    28     }
    29     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
    30     x*=y;
    31 }
    32 void o(long long x)
    33 {
    34     if(x<0)
    35     {
    36         p('-');
    37         x=-x;
    38     }
    39     if(x>9)o(x/10);
    40     p(x%10+'0');
    41 }
    42 int main()
    43 {
    44 //    freopen("t.in","r",stdin);
    45     while(cin>>a[++cnt])
    46     {
    47     if(a[cnt]>='A'&&a[cnt]<='Z')a[cnt]+='a'-'A';
    48     if(a[cnt]=='h')
    49     {
    50     f[1]++;
    51     f[1]%=mod;    
    52     }
    53     for(register long long j=10;j>=2;j--)
    54     if(a[cnt]==b[j])f[j]=(f[j-1]+f[j])%mod;    
    55     }    
    56     o(f[10]%mod);
    57      return 0;
    58 }

     其实没必要开数组的。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<ctime>
     7 #include<set>
     8 #include<map>
     9 #include<stack>
    10 #include<cstring>
    11 #define inf 2147483647
    12 #define For(i,a,b) for(register int i=a;i<=b;i++)
    13 #define p(a) putchar(a)
    14 #define g() getchar()
    15 #define mod 1000000007
    16 //by war
    17 //2017.11.8
    18 using namespace std;
    19 int f[20];
    20 char a[20]="@helloworld",x;
    21 void in(int &x)
    22 {
    23     int y=1;
    24     char c=g();x=0;
    25     while(c<'0'||c>'9')
    26     {
    27     if(c=='-')
    28     y=-1;
    29     c=g();
    30     }
    31     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
    32     x*=y;
    33 }
    34 void o(int x)
    35 {
    36     if(x<0)
    37     {
    38         p('-');
    39         x=-x;
    40     }
    41     if(x>9)o(x/10);
    42     p(x%10+'0');
    43 }
    44 int main()
    45 {
    46 //    freopen("t.in","r",stdin);
    47     while(cin>>x)
    48     {
    49         if(x>='A'&&x<='Z')x=x-'A'+'a';
    50         if(x=='h')
    51         f[1]++;
    52         f[1]%=mod;
    53         for(register int i=10;i>=2;i--)
    54         if(a[i]==x)
    55         f[i]=(f[i]+f[i-1])%mod;
    56     }
    57     o(f[10]%mod);
    58      return 0;
    59 }
  • 相关阅读:
    pytorch基础(4)-----搭建模型网络的两种方法
    Java
    Tools
    Maven
    Java
    DevOps
    Linux
    Java
    Java
    Nutch 使用总结
  • 原文地址:https://www.cnblogs.com/war1111/p/7715362.html
Copyright © 2011-2022 走看看