第 46 届 ICPC 国际大学生程序设计竞赛亚洲区域赛(沈阳)
edgnb
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int main(){ 6 string s; 7 cin>>s; 8 int t=0; 9 for(int i=0;i<s.size();i++){ 10 if((i+4<s.size())&&s[i]=='e'&&s[i+1]=='d'&&s[i+2]=='g'&&s[i+3]=='n'&&s[i+4]=='b') 11 t++; 12 } 13 cout<<t; 14 return 0; 15 }
看懂题目暴力模拟就行
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define pb push_back 5 #define int long long 6 int vis[166],mp[166]; 7 set<int> p; 8 9 signed main(){ 10 string ans=""; 11 int n; 12 string s; 13 cin>>n>>s; 14 for(int i=0;i<s.size();i++){ 15 string u=s; 16 string t=u.substr(0,i+1); 17 // cout<<i<<" "<<t<<" "; 18 memset(vis,0,sizeof(vis)); 19 memset(mp,0,sizeof(mp)); 20 p.clear(); 21 for(int j=t.size()-1;j>=0;j--){ 22 if(!mp[t[j]-'a']){ 23 int q=p.size(); 24 vis[t[j]-'a']=q; 25 mp[t[j]-'a']=1; 26 } 27 p.insert((int)(t[j]-'a')); 28 } 29 string cur=""; 30 for(int j=0;j<t.size();j++){ 31 cur=cur+((char)(vis[t[j]-'a']+'a')); 32 } 33 // cout<<cur<<endl; 34 if(cur>ans) ans=cur; 35 } 36 cout<<ans; 37 return 0; 38 }
思路:按每一位进行考虑,因为每一位都是独立的,互不影响。我们就可以对每个联通块(假设大小为N)进行遍历,假设第一个遍历的节点的i位取1,那么如果在符合的情况下就能得到连通块该记为1的数量S,那么就猜个结论:联通块最小1的个数MINX=min(S,N-S)。那么这题就能愉快的AC了!
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define pb push_back 5 #define int long long 6 const int N = 2e5+100 ; 7 int n,m; 8 int u[N],v[N],w[N],f[N]; 9 int col[N]; 10 int vis[N]; 11 vector<pair<int,int> >g[N]; 12 queue<int> q; 13 signed main(){ 14 cin>>n>>m; 15 for(int i=1;i<=m;i++){ 16 cin>>u[i]>>v[i]>>w[i]; 17 g[u[i]].pb({v[i],w[i]}); 18 g[v[i]].pb({u[i],w[i]}); 19 } 20 //memset(col,-1,sizeof(col)); 21 int ans=0; 22 for(int j=0;j<=30;j++){ 23 memset(col,-1,sizeof(col)); 24 memset(vis,0,sizeof(vis)); 25 for(int i=1;i<=n;i++){ 26 if(vis[i]) continue; 27 while(q.size()) q.pop(); 28 q.push(i); 29 col[i]=0; 30 int s1=0,tot=0; 31 int F=1; 32 while(q.size()){ 33 int u=q.front(); 34 q.pop(); 35 if(vis[u]) continue; 36 vis[u]=1; 37 tot++; 38 for(auto x:g[u]){ 39 int to=x.first; 40 int w=x.second; 41 if(col[to]==-1){ 42 q.push(to); 43 if(((1<<j)&w)){ 44 if(col[u]==0){ 45 col[to]=1; 46 s1++; 47 }else{ 48 col[to]=0; 49 } 50 }else{ 51 if(col[u]==1){ 52 col[to]=1; 53 s1++; 54 }else{ 55 col[to]=0; 56 } 57 } 58 }else{ 59 if((1<<j)&w){ 60 if(!(col[u]^col[to])){ 61 F=0; 62 break; 63 } 64 }else{ 65 if((col[u]^col[to])){ 66 F=0; 67 break; 68 } 69 } 70 } 71 } 72 } 73 if(!F){ 74 cout<<"-1"; 75 return 0; 76 } 77 ans=ans+min(tot-s1,s1)*(1<<j); 78 } 79 } 80 cout<<ans; 81 82 return 0; 83 } 84 /* 85 86 */
先将前面那个变成0000(例如:{1234 2345} ==》{0000 1234}),然后BFS预处理一下。O(1)查询。
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 string f[]={"0001","0010","0100","1000","1100","0110","0011","1110","0111","1111"}; 6 struct str{ 7 string s; 8 int sum; 9 }; 10 queue<str> q; 11 12 map<string,int> mp,vis; 13 signed main(){ 14 string s="0000"; 15 q.push({s,0}); 16 while(q.size()){ 17 str p=q.front(); 18 q.pop(); 19 // cout<<"=-="<<endl; 20 if(mp[p.s]) continue; 21 mp[p.s]=1; 22 vis[p.s]=p.sum; 23 for(int i=0;i<10;i++){ 24 string t=p.s; 25 for(int j=0;j<4;j++){ 26 if(f[i][j]=='1'){ 27 int num=t[j]-'0'; 28 num++; 29 if(num==10) num=0; 30 t[j]=(num+'0'); 31 } 32 } 33 //cout<<t<<endl; 34 q.push({t,p.sum+1}); 35 t=p.s; 36 for(int j=0;j<4;j++){ 37 if(f[i][j]=='1'){ 38 int num=t[j]-'0'; 39 num--; 40 if(num==-1) num=9; 41 t[j]=('0'+num); 42 } 43 } 44 //cout<<t<<endl; 45 q.push({t,p.sum+1}); 46 } 47 } 48 int T=1; 49 cin>>T; 50 while(T--){ 51 string s,t; 52 cin>>s>>t; 53 string u=""; 54 for(int i=0;i<4;i++){ 55 int x=s[i]-'0'; 56 int y=t[i]-'0'; 57 if(x==y){ 58 u+="0"; 59 }else if(x<y){ 60 int cha=y-x; 61 u+=(cha+'0'); 62 }else{ 63 int cha=y-x+10; 64 u+=(cha+'0'); 65 } 66 } 67 cout<<vis[u]<<endl; 68 } 69 return 0; 70 }