题解
2023-08-25 14:36:17
发布于:广东
6阅读
0回复
0点赞
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=105;
const int mo=1e9+7;
map<string,int> var_map;
int sta[N],n;
int top,tot,cnt;
int hashit(string s)
{
int &re=var_map[s];
if (!re)re=++tot;
return re;
}
struct matrix
{
int a[N][N];
matrix(){}
matrix(int v)
{
memset(a,0,sizeof(a));
fo(i,0,tot)a[i][i]=v;
}
int* operator [] (int i)
{
return a[i];
}
friend matrix operator *(matrix x,matrix y)
{
matrix c(0);
fo(i,0,tot)
fo(j,0,tot)
fo(k,0,tot)
c[i][j]=(c[i][j]+1ll*x[i][k]*y[k][j]%mo)%mo;
return c;
}
friend matrix pow(matrix a,int b)
{
matrix r(1);
while (b)
{
if (b&1)r=r*a;
a=a*a;
b>>=1;
}
return r;
}
}mat[N];
int main()
{
string buffer;
new(&mat[0])matrix(1);
while (1)
{
static char s[N*5];
gets(s);
istringstream stream(s);
stream>>s;
if (s[0]>='a'&&s[0]<='z')
{
static int b[N];
int left=hashit(s);
memset(b,0,sizeof(b));
while (stream>>s)
{
if (s[0]>='a'&&s[0]<='z')
{
int right=hashit(s);
fo(i,0,tot)
b[i]=(b[i]+mat[top][right][i])%mo;
}
else if (s[0]>='0'&&s[0]<='9')
{
int right=atoi(s);
b[0]=(b[0]+right)%mo;
}
}
fo(i,0,tot)mat[top][left][i]=b[i];
}
else if (s[0]>='0'&&s[0]<='9')
{
sta[++top]=atoi(s);
new (&mat[top])matrix(1);
}
else if (s[0]=='}')
{
mat[top-1]=pow(mat[top],sta[top])*mat[top-1];
top--;
}
else if (s[0]=='R')
{
stream>>s;
int val=hashit(s);
int ans=0;
fo(i,0,tot)ans=(ans+mat[0][val][i])%mo;
cout<<ans<<'\n';
break;
}
}
return 0;
}
这里空空如也
有帮助,赞一个