思路:显然要回到起点,U D次数要相同。L R同理。所以对于U D,选择min(u,d)次U操作和min(u,d)次D操作。L R 同理。而当 UD 操作次数为0时。 最多只能往左右走一格 就得回来。同理当 LR次数为0,只能往上一格再回来。剩下的合法的操作就按照 左上右下,绕一大圈,就可以回去了。
AC代码:
#include<iostream>#include<bits/stdc++.h>#include<unordered_map>//#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =1e6+1;int n,m,k,t =1,cas =1;int a[N],b[N];int res[N];signedmain(){
cin>>t;while(t--){
string s;
cin>>s;int u =0,d =0,l =0,r =0;for(int i =0; i < s.size(); i ++){if(s[i]=='U') u ++;if(s[i]=='D') d ++;if(s[i]=='R') r ++;if(s[i]=='L') l ++;}int remu =min(u,d);int reml =min(l,r);if(remu ==0&& reml ==0){
cout<<0<<endl<<endl;}elseif(remu ==0){
cout<<2<<endl;
cout<<"LR"<<endl;}elseif(reml ==0){
cout<<2<<endl;
cout<<"UD"<<endl;}else{
cout<<remu*2+reml*2<<endl;for(int i =0; i < remu ; i ++){
cout<<"U";}for(int i =0; i < reml ; i ++){
cout<<"L";}for(int i =0; i < remu ; i ++){
cout<<"D";}for(int i =0; i < reml ; i ++){
cout<<"R";}
cout<<endl;}}return0;}
#include<iostream>#include<bits/stdc++.h>#include<unordered_map>#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =1e6+1;int n,m,k,t =1,cas =1;int a[N],b[N];int res[N];signedmain(){//cin>>t;while(t--){
cin>>n>>m;
string s;
cin>>s;
map<char,int> mp;for(int i =0; i < m ; i ++){char x;
cin>>x;
mp[x]=1;}int res =0;for(int i =0; i < s.size(); i ++){int tmp =0;while(i < s.size()&& mp[s[i]]){
tmp ++;
i ++;}
res += tmp*(tmp+1)/2;}
cout<<res<<endl;}return0;}
#include<iostream>#include<bits/stdc++.h>#include<unordered_map>#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =1e6+1;int n,m,k,t =1,cas =1;int a[N],b[N];int res[N];int dp[N][2];signedmain(){//cin>>t;while(t--){
cin>>n;for(int i =0; i < n ; i ++){
cin>>a[i];}
dp[0][0]=1;for(int i =1; i < n ; i ++){if(a[i]> a[i-1]){
dp[i][0]= dp[i-1][0]+1;}else{
dp[i][0]=1;}}
dp[n-1][1]=1;for(int i = n-2; i >=0; i --){if(a[i]< a[i+1]){
dp[i][1]= dp[i+1][1]+1;}else{
dp[i][1]=1;}}int res =1;
a[n]=-1;for(int i =0; i < n-1; i ++){if(a[i]< a[i+1]){
res =max(res,dp[i][0]+dp[i+1][1]);}if(a[i]< a[i+2]){
res =max(res,dp[i][0]+dp[i+2][1]);}}
cout<<res<<endl;}return0;}
#include<iostream>#include<bits/stdc++.h>#include<unordered_map>#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =1e6+1;int n,m,k,t =1,cas =1;int a[N],b[N];int res[N];
vector<int> edge[N];struct node{int pos;int step;node(int x,int y){
pos = x;
step = y;}};
queue<node> que;int mark[N];int vis[N];boolcheck(int x,int y){return(a[x]%2)!=(a[y]%2);}signedmain(){//cin>>t;while(t--){
cin>>n;for(int i =0; i < n ; i ++){
cin>>a[i];}for(int i =0; i < n ; i ++){int l = i-a[i];int r = i+a[i];int flag =0;if(r < n ){if(check(i,r)) flag =1;
edge[r].push_back(i);}if(l >=0){if(check(i,l)) flag =1;
edge[l].push_back(i);}if(flag){
mark[i]=1;
que.push(node(i,1));}}while(!que.empty()){
node now = que.front();que.pop();int pos = now.pos;for(int i =0; i < edge[pos].size(); i ++){int to = edge[pos][i];if(mark[to]==0){
mark[to]= mark[pos]+1;
que.push(node(to,now.step+1));}}}for(int i =0; i < n ; i ++){if(!mark[i]) cout<<-1<<" ";else cout<<mark[i]<<" ";}
cout<<endl;}return0;}
node now = que.front(); que.pop();int x,y,z;
x = now.x +(now.x < n && s1[now.x]=='(');
y = now.y +(now.y < m && s2[now.y]=='(');
z = now.z +1;if(z <= N && dp[x][y][z]== inf){
dp[x][y][z]= dp[now.x][now.y][now.z]+1;
pre[x][y][z]=node(now.x,now.y,now.z,'(');
que.push(node(x,y,z,0));}
AC代码:
#include<iostream>#include<bits/stdc++.h>#include<unordered_map>//#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint inf =0x3f3f3f3f;constint N =2e2+7;int n,m,k,t =1,cas =1;int a[N],b[N];struct node{int x,y,z;char val;node(){}node(int xx,int yy,int zz,char v){
x = xx;
y = yy;
z = zz;
val = v;}}pre[N][N][N];int dp[N][N][N];
queue<node> que;
string s1,s2;voidbfs(){memset(dp,inf,sizeof(dp));
dp[0][0][0]=0;
que.push(node(0,0,0,0));while(!que.empty()){
node now = que.front(); que.pop();int x,y,z;
x = now.x +(now.x < n && s1[now.x]=='(');
y = now.y +(now.y < m && s2[now.y]=='(');
z = now.z +1;if(z <= N && dp[x][y][z]== inf){
dp[x][y][z]= dp[now.x][now.y][now.z]+1;
pre[x][y][z]=node(now.x,now.y,now.z,'(');
que.push(node(x,y,z,0));}
x = now.x +(now.x < n && s1[now.x]==')');
y = now.y +(now.y < m && s2[now.y]==')');
z = now.z -1;if(z >=0&& dp[x][y][z]== inf){
dp[x][y][z]= dp[now.x][now.y][now.z]+1;
pre[x][y][z]=node(now.x,now.y,now.z,')');
que.push(node(x,y,z,0));}}}signedmain(){//cin>>t;while(t--){
cin>>s1>>s2;
n = s1.size();m = s2.size();bfs();char res[500];int pos =0;
node now = pre[n][m][0];
res[pos++]= now.val;while(now.x+now.y+now.z){
now = pre[now.x][now.y][now.z];
res[pos++]= now.val;}
res[pos]=0;reverse(res,res+pos);
cout<<res<<endl;}return0;}/***
***/