Codeforces Round #349 (Div. 1)
2016-04-30 18:39
543 查看
A. Reberland Linguistics
此题重在理解”in a row”的含义。意思是不能有连续相同的两截。正确理解了题意以后,随便dp一下,答案塞到set里面即可。#include <bits/stdc++.h> #include <unordered_map> using namespace std; bool start[4][11111]; int main(){ string str; cin>>str; set<string> ans; start[2][str.size()] = 1; start[3][str.size()] = 1; for(int i=str.size()-1;i>=5;i--){ string tmp2 = str.substr(i,2); if(tmp2.size()==2){ if(tmp2!=str.substr(i+2,2) && start[2][i+2]){ ans.insert(tmp2); start[2][i] = 1; } if(start[3][i+2]){ ans.insert(tmp2); start[2][i] = 1; } } string tmp3 = str.substr(i,3); if(tmp3.size()==3){ if(tmp3!=str.substr(i+3,3) && start[3][i+3]){ ans.insert(tmp3); start[3][i] = 1; } if(start[2][i+3]){ ans.insert(tmp3); start[3][i] = 1; } } } cout<<ans.size()<<endl; for(string s:ans){ cout<<s<<endl; } return 0; }
B. World Tour
先求所有点对间最短路,然后计算距离每个点前三远的点(包括两个方向)。最后枚举中间两个点,再枚举第1和第4个点(最多枚举3个),找最长的。#include <bits/stdc++.h> #include <unordered_map> using namespace std; #define ll long long const int maxn = 3010; const int maxm = 5010; const ll INF = 1e9; int n,m,s,t; int head[maxn]; int pre[maxm]; int cnt; void init(){ memset(head,-1,sizeof(head)); cnt=1; } void addedge(int u,int v){ pre[cnt]=head[u]; head[u]=cnt; cnt++; } int a[maxm]; int b[maxm]; int d[maxn][maxn]; bool vis[maxn]; void bfs(int s){ for(int i=1;i<=n;i++){ d[s][i]=INF; vis[i] = 0; } d[s][s]=0; queue<int> pq; pq.push(s); while(pq.size()){ int cur = pq.front(); pq.pop(); if(vis[cur])continue; for(int i=head[cur];i!=-1;i=pre[i]){ int v=b[i]; ll tmp=d[s][v]; if(d[s][v] == INF){ d[s][v]=d[s][cur]+1; pq.push(v); } } vis[cur]=1; } } int bestfrom[maxn][4]; int bestto[maxn][4]; int main(){ init(); cin>>n>>m; for(int i=1;i<=m;i++){ scanf("%d%d",&a[i],&b[i]); addedge(a[i],b[i]); } for(int i=1;i<=n;i++){ bfs(i); } memset(bestfrom,-1,sizeof(bestfrom)); memset(bestto,-1,sizeof(bestto)); //bestfrom and bestto for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(d[j][i]!=INF){ bestfrom[i][3] = j; for(int k=3;k>0;k--){ if( bestfrom[i][k-1] == -1 || d[bestfrom[i][k]][i] > d[bestfrom[i][k-1]][i]){ swap(bestfrom[i][k],bestfrom[i][k-1]); } } } if(d[i][j]!=INF){ bestto[i][3] = j; for(int k=3;k>0;k--){ if( bestto[i][k-1] == -1 || d[i][bestto[i][k]] > d[i][bestto[i][k-1]]){ swap(bestto[i][k],bestto[i][k-1]); } } } } } int ans = 0; int res[5]; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j)continue; if(d[i][j] != INF){ int ij = d[i][j]; int s = -1; int t = -1; int tmp = 0; for(int ks = 0;ks<3;ks++){ for(int kt = 0;kt<3;kt++){ int tmps = bestfrom[i][ks]; int tmpt = bestto[j][kt]; if(tmps==-1 || tmpt==-1){ continue; } if(tmps==tmpt||tmps==i||tmps==j||tmpt==i||tmpt==j){ continue; } int tmpp = ij + d[tmps][i] + d[j][tmpt]; if(tmpp>tmp){ tmp = tmpp; s = tmps; t = tmpt; } } } if(tmp>ans){ ans = tmp; res[0] = s; res[1] = i; res[2] = j; res[3] = t; } } } } //cout<<ans<<endl; for(int i=0;i<4;i++){ cout<<res[i]<<" "; } return 0; }
C. Codeword
手速太慢了,没时间了,日后补。相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B