P2246 SAC#1 - Hello World(升级版)

时间:2023-03-09 16:33:28
P2246 SAC#1 - Hello World(升级版)

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]之前更新了吗?
唉,不要惯性思维啊,倒着匹配不就完了,是不?

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<map>
#define mod 1000000007
#define For(i,a,b) for(register long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.23
using namespace std;
char a[],b[]="@helloworld";
long long cnt;
long long f[];
void in(long long &x)
{
long long y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(long long x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
// freopen("t.in","r",stdin);
while(cin>>a[++cnt])
{
if(a[cnt]>='A'&&a[cnt]<='Z')a[cnt]+='a'-'A';
if(a[cnt]=='h')
{
f[]++;
f[]%=mod;
}
for(register long long j=;j>=;j--)
if(a[cnt]==b[j])f[j]=(f[j-]+f[j])%mod;
}
o(f[]%mod);
return ;
}

其实没必要开数组的。

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
#define mod 1000000007
//by war
//2017.11.8
using namespace std;
int f[];
char a[]="@helloworld",x;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=(x<<)+(x<<)+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
// freopen("t.in","r",stdin);
while(cin>>x)
{
if(x>='A'&&x<='Z')x=x-'A'+'a';
if(x=='h')
f[]++;
f[]%=mod;
for(register int i=;i>=;i--)
if(a[i]==x)
f[i]=(f[i]+f[i-])%mod;
}
o(f[]%mod);
return ;
}