-
B - Blown Garland
CodeForces - 758B - Nothing is eternal in the world
- if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them.
- 仔细分析后发现四个字母为一个循环节。
-
#include<bits/stdc++.h> using namespace std; int main() { string str; map<char,int>mmp; char son[5]; cin>>str; for(int i=0; i<str.size(); i++) if(str[i]!='!') son[(i+1)%4]=str[i]; for(int i=0; i<str.size(); i++) if(str[i]=='!') mmp[son[(i+1)%4]]++; cout<<mmp['R']<<" "<<mmp['B']<<" "<<mmp['Y']<<" "<<mmp['G']<<endl; return 0; }
#include <bits/stdc++.h> #define maxn 105 using namespace std; int a[maxn],n,cnt=0,len; string str; char son[maxn]; string to="RBYG"; int temp1,temp2,temp3,temp4; bool judge(int t) { temp1=temp2=temp3=temp4=0; if((t-3)>=0) { for(int k=t; k>=t-3; k--) { if(str[k]=='R') temp1++; else if(str[k]=='B') temp2++; else if(str[k]=='Y') temp3++; else if(str[k]=='G') temp4++; } if(temp1>1||temp2>1||temp3>1||temp4>1) return 0; } temp1=temp2=temp3=temp4=0; if(t+3<len) { for(int k=t; k<=t+3; k++) { if(str[k]=='R') temp1++; else if(str[k]=='B') temp2++; else if(str[k]=='Y') temp3++; else if(str[k]=='G') temp4++; } if(temp1>1||temp2>1||temp3>1||temp4>1) return 0; } temp1=temp2=temp3=temp4=0; if(t+2<len&&t-1>=0) { for(int k=t-1; k<=t+2; k++) { if(str[k]=='R') temp1++; else if(str[k]=='B') temp2++; else if(str[k]=='Y') temp3++; else if(str[k]=='G') temp4++; } if(temp1>1||temp2>1||temp3>1||temp4>1) return 0; } temp1=temp2=temp3=temp4=0; if(t+1<len&&t-2>=0) { for(int k=t-2; k<=t+1; k++) { if(str[k]=='R') temp1++; else if(str[k]=='B') temp2++; else if(str[k]=='Y') temp3++; else if(str[k]=='G') temp4++; } if(temp1>1||temp2>1||temp3>1||temp4>1) return 0; } return 1; } void dfs(int x,int index) { if(index==cnt) { temp1=temp2=temp3=temp4=0; for(int i=0; i<index; i++) { if(son[i]=='R') temp1++; else if(son[i]=='B') temp2++; else if(son[i]=='Y') temp3++; else if(son[i]=='G') temp4++; } cout<<temp1<<" "<<temp2<<" "<<temp3<<" "<<temp4<<endl; return; } for(int i=0; i<4; i++) { str[a[x]]=to[i]; if(judge(a[x])) { son[index]=to[i]; dfs(x+1,index+1); str[a[x]]='!'; } else str[a[x]]='!'; } } int main() { cin>>str; len=str.size(); for(int i=0; i<len; i++) if(str[i]=='!') a[cnt++]=i; if(cnt>0) dfs(0,0); else cout<<"0 0 0 0"<<endl; return 0; }