现在给你一个只包含""括号的字符串,问你至少加多少个字符是合法的,对于字符串合法的定义为:
#include
using namespace std;
int dp[205][205];
int main(){
string s;
int i,j,k,p;
while(cin>>s){
memset(dp,0,sizeof(dp));
if(s.size()==1){
puts("1");
continue;
}
for(i=0;i+1<s.size();i++){
if((s[i]=='('&&s[i+1]==')')||(s[i]=='['&&s[i+1]==']'))
dp[i][i+1]=2;
}
for(i=3;i<=s.size();i++){
for(j=0;j+i-1<s.size();j++){
k=j+i-1;
dp[j][k]=max(dp[j][k],dp[j+1][k-1]);
dp[j][k]=max(dp[j][k],max(dp[j+1][k],dp[j][k-1]));
if((s[j]=='('&&s[k]==')')||(s[j]=='['&&s[k]==']')){
dp[j][k]=max(dp[j][k],dp[j+1][k-1]+2);
}
for(p=j+1;p<k;p++){
dp[j][k]=max(dp[j][k],dp[j][p]+dp[p+1][k]);
}
}
}
cout<<s.size()-dp[0][s.size()-1]<<endl;
}
return 0;
}
#include
using namespace std;
double calc(int a, int b, double x) {
return 1.0 * a * x * x + x + b;
}
int main() {
int T; cin >> T;
while (T--) {
int a, b, c,d;
cin >> a >> b >> c >> d;
double mid = (c + d) / 2.0;
double y1 = calc(a, b, c);
double y2 = calc(a, b, mid);
double y3 = calc(a, b, d);
printf("%.6lf\n", 1.0/6 * (d - c) * (y1 + 4 * y2 + y3));
}
return 0;
}
#include
using namespace std;
#define ll long long
const ll mod = 1000000000 + 7;
ll poww(ll base, ll a) {
ll ans = 1;
while (a != 0) {
if (a & 1) {
ans *= base;
ans %= mod;
}
base *= base;
base %= mod;
a >>= 1;
}
return ans;
}
int main() {
int n;
while (cin >> n) {
cout << (n* poww(2, n-1)) % mod << endl;
}
return 0;
}
#include
using namespace std;
const int siz=100005;
vector<int> G[siz];
map<int,int> mp;
int main(){
int n,m,i,j,k,x,y,ans,tmp;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=0;i<=n;i++)
G[i].clear();
for(i=0;i<m;i++){
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
ans=0;
mp.clear();
for(i=1;i<=n;i++){
for(j=0;j<G[i].size();j++)
mp[i]+=(G[i][j]*G[i][j]);
}
for(i=1;i<=n;i++){
if(G[i].size()<=1)
continue;
for(j=0;j<G[i].size();j++){
for(k=j+1;k<G[i].size();k++){
if(G[G[i][j]].size()!=G[G[i][k]].size())
continue;
if(mp[G[i][j]]==mp[G[i][k]])
ans++;
}
}
}
printf("%lld\n",ans);
}
return 0;
}
#include
using namespace std;
const int maxn = 2002;
const int INF = 0x3f3f3f3f;
struct Edge {
int u, v, w;
int next;
}E[maxn *100];
int head[maxn];
int cnt;
void init() {
memset(head, -1, sizeof(head));
cnt = 0;
}
void add_edge(int u, int v, int w) {
E[cnt].u = u;
E[cnt].v = v;
E[cnt].w = w;
E[cnt].next = head[u];
head[u] = cnt++;
}
int n;
int vis[maxn];
int dis[maxn];
int gao(int s, int t){
for(int i = 1;i <= n;i++) vis[i] = 0;
for(int i = 1;i <= n;i++) dis[i] = (i == s ? 0 : INF);
dis[1] = 0;
for(int i = 1;i <= n;i++) {
int x, minn = INF;
for(int j = 1;j <= n;j++){
if(!vis[j] && dis[j] <= minn){
x = j;
minn = dis[j];
}
}
vis[x] = 1;
for(int j = head[x]; j != -1; j = E[j].next){
int y = E[j].v;
int w = E[j].w;
dis[y] = min(dis[y], dis[x] + w);
}
}
if (dis[n] == INF) return -1;
return dis[n];
}
int main() {
int m, k;
while (cin >> n >> m >> k) {
init();
int a, b, w;
while (m--) {
cin >> a >> b >> w;
add_edge(a, b, w);
add_edge(b, a, w);
}
while (k--) {
cin >> a >> b;
add_edge(a, b, 0);
}
cout << gao(1, n) << endl;
}
return 0;
}