我这个分治的思路 //C #pragma GCC optimize(2) (1282)#include <bits> using namespace std; typedef long long ll; typedef pair<int> pii; typedef vector<int> vi; const int maxn = 4e5 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f; char s[maxn]; int n, match[maxn]; ll dfs(int l, int r){ if(l + 1 == r) return 2; int now = l; ll ans = 1; if(match[l] == r){ ans = (dfs(l+1, r-1) + 1) % mod; }else { while(now <= r){ ans = (ans * dfs(now, match[now])) % mod; now = match[now] + 1; } } return ans % mod; } void init(){ stack<int> st; for(int i = 1; i <= n; i++){ if(s[i] == '(') st.push(i); else { match[st.top()] = i; st.pop(); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> (s + 1); n = strlen(s + 1); init(); cout << dfs(1, n) << '\n'; return 0; }</int></int></int></bits>