题解:
第一题:简单模拟:
#includeusing namespace std;const int M = 500000;// up;char s[M], ans[M];int main(){ freopen("expression.in","r",stdin); freopen("expression.out","w",stdout); int tot = -1; scanf("%s", s); int len = strlen(s); for(int i = 0; i < len;){ if(s[i] == '+'){ ans[++tot]='+';i++; while(i ='0'&&s[i]<='9')ans[++tot]=s[i++]; } if(i==len)break; if(s[i] == '-'){ ans[++tot]='-'; i++; int t=i; if(s[i]=='0'){ans[++tot]='0';i++;continue;} if(s[i]!='0'){ ans[++tot]=s[i++]; if(i==len)break; int t=i; while(i ='0'&&s[i]<='9'){ ans[++tot]=s[i++]; } } if(i==len)break; } } if(s[i]<='9'&&s[i]>='0'&&i==0){ while(i <='9'&&s[i]>='0') ans[++tot]=s[i++]; } } printf("%s", ans);}
第二题:
#includeusing namespace std;const int M = 400005, N = 100005;int h[N], fa[N], tot = 1, dep[N], cnto, cnte, even[N], odd[N];bool vis[M];struct edge{ int v, nxt;}G[M];struct node{ int u, v, id;}g[M];void add(int u, int v){ G[++tot].v =v, G[tot].nxt = h[u], h[u] = tot;}int dfs1(int u, int f){ dep[u] = dep[f] + 1; for(int i = h[u]; i; i = G[i].nxt){ if(vis[i])continue; int v = G[i].v; vis[i] = vis[i^1] = 1; if(dep[v]){ if((dep[v]&1) == (dep[u]&1)){ odd[u]++,odd[v]--,cnto++; } else even[u]++, even[v]--,cnte++; } else { fa[v] = i; dfs1(v, u); } } }void dfs2(int u){ for(int i = h[u]; i; i = G[i].nxt){ if(i == fa[G[i].v]){ int v=G[i].v; dfs2(v); odd[u] += odd[v], even[u] += even[v]; } }}int read(){ int x = 0; int f = 1; char c = getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1;c=getchar();} while(c<='9'&&c>='0'){x=x*10+c-'0';c=getchar();} return x*=f;}int main(){ freopen("voltage.in","r",stdin); freopen("voltage.out","w",stdout); int n = read(), m = read(); int ans = 0; for(int i = 1; i <= m; i++){ int u = read(), v = read(); add(u, v); add(v, u); } dfs1(1, 0); dfs2(1); for(int i = 2; i <= n; i++) if(!even[i] && odd[i]==cnto) ans++; if(cnto == 1) ans++; printf("%d\n", ans);}
第三题:
#include#include #include using namespace std ; int L , N , jl[2005] , jr[2005] , rg[2005] , cok[2] , nw , ans , nd1 , nd2 ; char ty[2005] ; void insert ( int l , int r ) { while ( rg[l] ) { if ( rg[l] > r ) { int ll = l ; l = r + 1 ; swap ( rg[ll] , r ) ; } else if ( rg[l] < r ) l = rg[l] + 1 ; if ( rg[l] == r || l >= L ) return ; } rg[l] = r ; }int zh ( char x ) { if ( x == '(' ) return 0 ; return 1 ; }void gao ( int l , int r ) { int mx = 0 ; while ( nw <= r ) { if ( ty[nw] == '(' ) mx ++ ; else if ( ty[nw] == ')' ) { if ( mx ) mx-- ; else { nd1++ ; mx++ ; } } nw++ ; if ( rg[nw] ) { if ( rg[nw] > r && nw <= r ) { insert ( r + 1 , rg[nw] ) ; insert ( nw , r ) ; } gao ( nw , rg[nw] ) ; } } if ( mx % 2 ) ans = -1e9 ; nd2 += mx / 2 ;}int main ( ){ freopen ( "parentheses.in" , "r" , stdin ) ; freopen ( "parentheses.out" , "w" , stdout ) ; scanf ( "%s" , &ty ) ; L = strlen ( ty ) ; scanf ( "%d" , &N ) ; for ( int i = 1 ; i <= N ; i++ ) scanf ( "%d" , &jl[i] ) ; for ( int i = 1 ; i <= N ; i++ ) scanf ( "%d" , &jr[i] ) ; for ( int i = 1 ; i <= N ; i++ ) { if ( ( jr[i] - jl[i] + 1 ) % 2 ) { printf ( "-1\n" ) ; return 0 ; } insert ( jl[i] , jr[i] ) ; } while ( nw < L ) { if ( !rg[nw] ) cok[zh ( ty[nw++] )]++ ; else gao ( nw , rg[nw] ) ; } while ( nd1 && nd2 ) ans ++ , nd1 -- , nd2 -- ; if ( cok[0] < nd1 || cok[1] < nd2 ) ans = -1e9 ; ans += ( nd1 + nd2 ) ; ans < 0 ? printf ( "-1\n" ) : printf ( "%d\n" , ans ) ; }