标签:a* 字符 example vector line enc 代码 form std
S (((()()())))
P-sequence 4 5 6666
W-sequence 1 1 1456
Input
Output
Sample Input
2 6 4 5 6 6 6 6 9 4 6 6 6 6 8 9 9 9
Sample Output
1 1 1 4 5 6 1 1 2 4 5 1 1 3 9
思路:
先将数字转换为字符串,然后只要判断出现右括号时往前推找到最近的左括号,标记下左括号代表已经和右括号结合过了,同时统计下在最近的没标记过的左括号之间有几个被标记过的,加起来
就是包含的括号数。题目没什么坑点,想到思路就能做出来了
实现代码:
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<set> #include<list> using namespace std; #define ll long long const int Mod = 1e9+7; const int inf = 1e9; const int Max = 1e5+10; vector<int>vt[Max]; //void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}} //ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;} ??? //int gcd(int a,int b) { return (b>0)?gcd(b,a%b):a; } ??С??? //int lcm(int a, int b) { return a*b/gcd(a, b); } ??С???? int main() { int n,m,i,j,a[50],c[50],vis[50],ans; char b[50]; cin>>n; while(n--){ cin>>m; memset(vis,0,sizeof(vis)); for(i=0;i<m;i++) cin>>a[i]; for(i=0;i<2*m;i++) b[i] = ‘(‘; for(i=0;i<m;i++) b[a[i]+i] = ‘)‘; for(i=0;i<m;i++){ ans = 0; for(j=a[i]+i-1;j>=0;j--){ if(b[j]==‘(‘&&vis[j]==1) ans++; else if(b[j]==‘(‘&&vis[j]==0){ vis[j]=1; ans++; c[i] = ans; break; } } } for(i=0;i<m-1;i++) cout<<c[i]<<" "; cout<<c[m-1]<<endl; } return 0; }
标签:a* 字符 example vector line enc 代码 form std
原文地址:http://www.cnblogs.com/kls123/p/7395541.html