• Canada Cup 2016 C. Hidden Word


    C. Hidden Word
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:

    ABCDEFGHIJKLM
    NOPQRSTUVWXYZ

    We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.

    A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).

    You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).

    Input

    The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.

    Output

    Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".

    Examples
    input
    ABCDEFGHIJKLMNOPQRSGTUVWXYZ
    output
    YXWVUTGHIJKLM
    ZABCDEFSRQPON
    input
    BUVTYZFQSNRIWOXXGJLKACPEMDH
    output
    Impossible

    思路:先将初始字符串t删去重复的字母,然后按初始的顺序枚举可能的字符组合(26种),将其分成两行,判断是否符合条件即可。
    总结:这是一道思维题,简单搜索的部分,也是对编码功底的锻炼。写了(改了)好久,好久。。
    代码
     1 #include "cstdio"
     2 #include "algorithm"
     3 #include "cstring"
     4 #include "queue"
     5 #include "cmath"
     6 #include "vector"
     7 #include "map"
     8 #include "stdlib.h"
     9 #include "set"
    10 typedef long long ll;
    11 using  namespace std;
    12 const int N=1e4+5;
    13 const int mod=1e9+7;
    14 #define db double
    15 //int a[N];
    16 //set<int> q;
    17 //map<int ,int > u;
    18 //ll  dp[N];
    19 char t[30];
    20 char f[27];
    21 int m=1,pd;
    22 int a[26],b[26];
    23 char s[2][14],g[30];
    24 int d[8][2]={{1,1},{1,-1},{1,0},{-1,1},{-1,-1},{-1,0},{0,1},{0,-1}};//刚开始没带等号
    25 void dfs(int x,int y,int m){
    26     if(m==27) pd=1;//m写成26了,最后的错误
    27     a[s[x][y]-'A']--;
    28     for(int i=0;i<8;i++){
    29         int nx=x+d[i][0],ny=y+d[i][1];
    30         if(nx>=0&&nx<2&&ny>=0&&ny<13&&s[nx][ny]==g[m]&&a[s[nx][ny]-'A']>0) {
    31             dfs(nx, ny, m + 1);
    32         }
    33     }
    34 }
    35 int main()
    36 {
    37     scanf("%s",t);
    38     strcpy(g,t);
    39     char e,v;
    40     for(int i=0;i<27;i++){
    41         if(t[i]==t[i+1]) {puts("Impossible");return 0;}
    42         v=t[i];
    43         a[v-'A']++;//'A'写成'a'一直没查出来
    44         if(a[v-'A']==2) {
    45             e=v;
    46             for(int j=i+1;j<27;j++)
    47                 a[t[j]-'A']++;
    48             for(int j=i+1;j<27;){
    49                 t[i++]=t[j++];
    50             }
    51         }
    52     }
    53     for(int i=0;i<26;i++){
    54         b[i]=a[i];
    55         t[i+26]=t[i];
    56     }
    57     for(int i=0;i<26;i++){
    58         int p=0;
    59         for(int j=i;j<26+i;){
    60             f[p++]=t[j++];
    61         }
    62         int k=13;
    63         for(int l=0;l<13;l++) s[0][l]=f[l];//开始数组开13,开小了
    64         for(int l=12;l>=0;l--) s[1][l]=f[k++];
    65         pd=0;
    66         for(int ii=0;ii<2;ii++){
    67             for(int j=0;j<13;j++){
    68                 if(s[ii][j]==t[0]){
    69                     dfs(ii,j,1);
    70                     if(pd) {
    71                         for(int kk=0;kk<2;kk++)
    72                             puts(s[kk]);
    73                         return 0;
    74                     }
    75                 }
    76             }
    77         }
    78         memcpy(a,b, sizeof(a));
    79     }
    80     return 0;
    81 }
    
    
  • 相关阅读:
    学习官方示例 System.Assigned
    用 TBytesStream 类实现的读文件为十六进制字符的函数
    学习官方示例 System.Hi、System.Lo
    给 TComboBox 添加图标 回复 "heyongan" 的问题
    [每周特惠]WPF编程(第二版) + 设计模式—基于C#的工程化实现及扩展
    【公告】8月28日(周六)早上5:007:00服务器升级
    【网站公告】新Web服务器上线
    欢迎参加上海张江浦东软件园以及分园的技能培训课程
    上周热点回顾(8.309.5)
    上周热点回顾(8.168.22)
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6523181.html
Copyright © 2020-2023  润新知