比赛链接
- C
题意:
给你括号和一堆数,把他想象成一个数学表达式,A
表示1,B
表示2,以此类推,问最后的和是多少。
思路:
利用栈思想解决括号配对的问题。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=(int)1e5+100;
string s;
ll stk[N],top=0;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "r", stdin);
#endif
cin>>s;
for(int i=0; i<int(s.size()); ++i){
if(s[i]=='(') ++top;
else if(s[i]==')'){
ll now=0;
int j;
for(j=i+1; j<int(s.size()) && (s[j]>='0' && s[j]<='9'); ++j){
now=now*10+int(s[j]-'0');
}
if(now==0) now=1;
stk[top]*=now;
if(top-1>=0){
stk[top-1]+=stk[top];
stk[top]=0;
--top;
}
i=j-1;
} else{
ll now=0;
int j;
for(j=i+1; j<int(s.size()) && (s[j]>='0' && s[j]<='9'); ++j){
now=now*10+int(s[j]-'0');
}
if(now==0) now=1;
stk[top]+=now*int(s[i]-'A'+1);
i=j-1;
}
}
cout<<stk[0]<<'\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
- I
题意:
只能往右或下走,问走到右下角的路径权值和(mod 1e4+7)有多少种可能。
n,m<=100。
思路:
dp。
因为开不了1e8的数组,所以用bitset优化。
#include <bits/stdc++.h>
using namespace std;
const int N=105;
const int mod=(int)1e4+7;
int n,m,a[N][N];
bitset<mod+5>dp[N][N];
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "r", stdin);
#endif
cin>>n>>m;
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
cin>>a[i][j];
}
}
dp[0][0][a[0][0]%mod]=1;
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
for(int k=0; k<mod; ++k){
if(i+1<n && dp[i][j][k]==1) dp[i+1][j][(k+a[i+1][j])%mod]=1;
if(j+1<m && dp[i][j][k]==1) dp[i][j+1][(k+a[i][j+1])%mod]=1;
}
}
}
int ans=0;
for(int i=0; i<mod; ++i) if(dp[n-1][m-1][i]!=0) ++ans;
cout<<ans<<'\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}