(Canada Cup 2016 C) Hidden Word

来源:互联网 发布:阿里云系统看电视 编辑:程序博客网 时间:2024/05/21 03:59

(Canada Cup 2016 C) Hidden Word

题意:给你一个27大写字母的字符串,每个字母至少出现一次。要你能否构造一个2*13的图,保证给出的字符串是条通路,图通路的定义是:相邻或对角的两个能相通。
题解:根据题意,字符串肯定有一个大写字母出现两次,而这个字符就是关键,我们需要通过这个字符两次(记为G),将G的前后两个位置找到,把两个位置中间的字符放在尾部构成个环路,可以确定到中间部分的位置,在顺序排入剩下的字符。

#include <cstdio>#include <cstring>#include <iostream>#include <queue>#include <stack>using namespace std;char a[30];char da[2][13];int v[30];int main(){    while(~scanf("%s",a))    {        memset(v,0,sizeof(v));        int len = 27;        int l = -1, r = -1;        char two;        for(int i = 0; i < len; i++)        {            v[a[i]-'A'] ++;            if(v[a[i]-'A'] == 2)                two = a[i];        }        //printf("%c",two);        for(int i = 0; i < len; i++)        {            if(a[i] == two)            {                if(l == -1)                    l = i;                else                    r = i;            }        }        //printf("%d %d\n",l,r);        int cha = r-l;        if(cha == 1)        {            printf("Impossible\n");            continue;        }        int ans = (cha-1)/2;        da[0][12-ans] = two;        for(int i = l+1; i < len+l+1; i++)        {            int temp;            if(i == r)                continue;            if(i >= len)                temp = i-len;            else                temp = i;            ans--;            if(ans >= 0)                da[0][12-ans] = a[temp];            else if(ans < 0 && ans >= -13)                da[1][13+ans] = a[temp];            else if(ans < -13)                da[0][-14-ans] = a[temp];        }        for(int i = 0; i < 2; i++)        {            for(int j = 0; j < 13; j++)                printf("%c",da[i][j]);            printf("\n");        }    }    return 0;}
0 0
原创粉丝点击