Codeforces Round #349 (Div. 1)

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)