传送门
文章目录
- 题意:
- 思路:
题意:
给你一个长度为272727的字符串sss,其中262626个字母每种都至少出现一次,让你构造一个2×132×132×13的矩阵,使得每个字母都出现一次,并且存在一条路径,经过这个路径上的点组成的字符串是sss,你可与向888个方向走,有解输出矩阵,无解输出ImpossibleImpossibleImpossible。
思路:
一开始看错题了,卡了半天。
明显,sss串中恰好有两个字母相同,所以当相同的两个字母相邻的时候,这个时候是无解的。
否则一定有解,通过画一画不难发现,我们可以在第一行确定一个起点,让后向右走,走到头之后向下,依次这样蛇形的填满,由于我们需要经过相同的字母两次,所以可以设计这个字母出现在第一行,让后从第二行跳到这个位置,再跳到原本的下一个位置即可。所以我们一定存在一种蛇形填数是符合条件的。
有了上面的结论,直接枚举所有情况让后检查即可。
// Problem: F - Hidden Word
// Contest: Virtual Judge - Camp4
// URL: https://vjudge.net/contest/455435#problem/F
// Memory Limit: 262 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int c[30],ne[N];
char s[100];
vector<int>v[2];
PII p[N];
int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};bool check() {int x,y;for(int i=0;i<2;i++) {for(int j=0;j<13;j++) {if(v[i][j]==s[1]-'A') {x=i; y=j;continue;}}}int cnt=1;queue<PII>q; q.push({x,y});while(cnt<27&&q.size()) {PII u=q.front(); q.pop();for(int k=0;k<8;k++) {int dx=u.X+dir[k][0];int dy=u.Y+dir[k][1];if(dx<0||dy<0||dx>=2||dy>=13) continue;if(v[dx][dy]==s[cnt+1]-'A') {q.push({dx,dy});cnt++;break;}} }if(cnt==27) return true;else return false;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cin>>(s+1);int ch; bool flag=true;for(int i=1;i<=27;i++) {c[s[i]-'A']++;if(c[s[i]-'A']==2) {if(s[i-1]==s[i]) flag=false;}}if(!flag) {puts("Impossible");return 0;}for(int i=1;i<=27;i++) {if(c[s[i]-'A']) {if(v[0].size()<13) v[0].pb(s[i]-'A');else v[1].pb(s[i]-'A');}c[s[i]-'A']=0;}reverse(v[1].begin(),v[1].end());while(!check()) {vector<int>vv1,vv2;vv1.pb(v[1][0]);for(int i=0;i<12;i++) vv1.pb(v[0][i]);for(int i=1;i<13;i++) vv2.pb(v[1][i]);vv2.pb(v[0][12]);v[0]=vv1; v[1]=vv2;// for(auto x:v[0]) cout<<x<<' '; puts("");// for(auto x:v[1]) cout<<x<<' '; puts("");// puts("");}for(auto x:v[0]) cout<<(char)(x+'A'); puts("");for(auto x:v[1]) cout<<(char)(x+'A'); puts("");return 0;
}
/*ABCDEFGHIJKLM
NOPQRSGTUVWXYZABCDEFGHIJKLM
ZYXWVUTSRQPON*/