Canada Cup 2016 C 模拟

来源:互联网 发布:php如何实现常驻内存 编辑:程序博客网 时间:2024/06/05 08:28

题目传送门:http://codeforces.com/problemset/problem/725/C


题意:给一个27个字符的字符串,2联通,首位不联通。26个大写字母每个至少出现一次,然后构造一个23*2的网格,8联通,原串联通的字符在新串中也联通。相同字符无法联通


思路:如果原串有连续相同的字符,则无法构造。其他需要找到两个相同字符的位置,从它们两个中间折开。然后再折叠较长的那行串,使两串为2*13


代码如下:

#include <iostream>#include <algorithm>#include <cstring>#include <stdio.h>#include <string>#include <cmath>#include <queue>#include <set>#include <map>#include <stack>#include <bitset>#include <cstdlib>#include <vector>using namespace std;#define   lson          l,m,rt<<1#define   rson          m+1,r,rt<<1|1#define   ll            long long#define   ull           unsigned long long#define   mem(n,v)      memset(n,v,sizeof(n))#define   MAX           100005#define   MAXN          10005#define   PI            3.1415926#define   E             2.718281828459#define   opnin         freopen("input.txt","r",stdin)#define   opnout        freopen("output.txt","w",stdout)#define   clsin         fclose(stdin)#define   clsout        fclose(stdout)#define   haha1         cout << "haha1"<< endl#define   haha2         cout << "haha2"<< endl#define   haha3         cout << "haha3"<< endlconst int    INF    =   0x3f3f3f3f;const ll     INFF   =   0x3f3f3f3f3f3f3f3f;const double pi     =   3.141592653589793;const double inf    =   1e18;const double eps    =   1e-8;const ll     mod    =   1e18;const ull    mx     =   133333331;/**************************************************************************/int main(){//    opnin;//    opnout;    int vis[200];    mem(vis,-1);    string str;    cin >> str;    int l = 0,r = 0;    for(int i=0;i<str.size();i++){        if(vis[str[i]]!=-1){            l = vis[str[i]];            r = i;            break;        }        vis[str[i]] = i;    }    if(l == r-1){        cout << "Impossible" << endl;        return 0;    }    char ans1[100],ans2[100];    int mid = (l+r)/2;//    cout << "m,l,r " << mid << ' ' << l << ' ' << r << endl;    int i;    int cnt1 = 0;    for(i=mid+1;i<str.size();i++) ans1[cnt1++] = str[i];    ans1[cnt1] = 0;//    haha1;//    cout << cnt1 << ' ' << ans1 << endl;    int cnt2 =0;    for(i=mid;i>=0;i--){        if(i == l) continue;        ans2[cnt2++] = str[i];    }    ans2[cnt2] = 0;//    cout << cnt2 << ' ' << ans2 << endl;    if(cnt1 > cnt2){        for(int i=13;i<cnt1;i++){            ans2[25-i] = ans1[i];        }    }    else{        for(int i=13;i<cnt2;i++){            ans1[25-i] = ans2[i];        }    }    ans1[13] = 0;    ans2[13] = 0;//    haha3;    cout << ans1 << endl;    cout << ans2 << endl;//    clsin;//    clsout;    return 0;}


0 0
原创粉丝点击