模拟题,代码写得比较乱。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; char s[];
char tmp[];
char u[];
char Map[][];
bool flag[]; vector<int> ans;
int tot; void f()
{
tot = ;
for (int i = ; s[i]; i++){
if (s[i] >= 'a'&&s[i] <= 'z')
tmp[tot++] = s[i];
else if (s[i] >= 'A'&&s[i] <= 'Z')
tmp[tot++] = s[i] - 'A' + 'a';
} tmp[tot] = ;
for (int i = ; tmp[i]; i++)
if (tmp[i] == 'j') tmp[i] = 'i';
} void EYET()
{
for (int i = ; s[i]; i++){
if (s[i] >= 'a'&&s[i] <= 'z')
tmp[tot++] = s[i];
else if (s[i] >= 'A'&&s[i] <= 'Z')
tmp[tot++] = s[i] - 'A' + 'a';
} tmp[tot] = ;
for (int i = ; tmp[i]; i++)
if (tmp[i] == 'j') tmp[i] = 'i';
} void Fill()
{
tot = ;
memset(Map, , sizeof Map);
memset(flag, , sizeof flag);
for (int i = ; tmp[i]; i++)
{
// printf("%c %d \n", tmp[i], flag[tmp[i] - 'a']);
if (flag[tmp[i] - 'a'] == )
{
flag[tmp[i] - 'a'] = ; tot++;
int r, c;
if (tot % == ) r = tot / ;
else r = tot / + ;
c = tot % ;
if (c == ) c = ;
Map[r][c] = tmp[i];
}
} for (int i = ; i<; i++)
{
char sign = i + 'a';
if (sign == 'j') continue;
if (flag[i] == ) continue; tot++;
int r, c;
if (tot % == ) r = tot / ;
else r = tot / + ;
c = tot % ;
if (c == ) c = ;
Map[r][c] = sign;
}
} bool P1(char a, char b)
{ int posxA, posyA; int posxB, posyB;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (Map[i][j] == a) posxA = i, posyA = j;
if (Map[i][j] == b) posxB = i, posyB = j;
}
} if (posxA == posxB)
{
posyA++;
if (posyA > ) posyA = ; posyB++;
if (posyB > ) posyB = ; ans.push_back(Map[posxA][posyA] - 'a');
ans.push_back(Map[posxB][posyB] - 'a'); return ;
}
return ;
} bool P2(char a, char b)
{ int posxA, posyA; int posxB, posyB;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (Map[i][j] == a) posxA = i, posyA = j;
if (Map[i][j] == b) posxB = i, posyB = j;
}
} if (posyA == posyB)
{
posxA++;
if (posxA > ) posxA = ; posxB++;
if (posxB > ) posxB = ; ans.push_back(Map[posxA][posyA] - 'a');
ans.push_back(Map[posxB][posyB] - 'a'); return ;
}
return ;
} void P3(char a, char b)
{
int posxA, posyA; int posxB, posyB; for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (Map[i][j] == a) { posxA = i, posyA = j; }
if (Map[i][j] == b) { posxB = i, posyB = j; }
}
} ans.push_back(Map[posxA][posyB] - 'a');
ans.push_back(Map[posxB][posyA] - 'a');
} void work()
{
int len = strlen(tmp);
strcpy(u, tmp); queue<int>Q;
for (int i = ; i < len; i++) Q.push(tmp[i]-'a'); while (!Q.empty())
{
char sign1 = Q.front() + 'a';
char sign2;
Q.pop();
if (Q.empty()) break;
if (Q.front()+'a' == sign1)
{
sign2 = 'x';
if (P1(sign1, sign2) == ) continue;
else if (P2(sign1, sign2) == ) continue;
else P3(sign1, sign2);
}
else
{
sign2 = Q.front() + 'a';
Q.pop();
if (P1(sign1, sign2) == ) continue;
else if (P2(sign1, sign2) == ) continue;
else P3(sign1, sign2);
}
}
} int main()
{
while (gets(s))
{
ans.clear();
f();
Fill(); bool Exit = ; tot = ;
while ()
{
scanf("%s", s);
int len = strlen(s);
if (s[] == '*' || s[len - ] == '*') Exit = ;
EYET();
if (Exit) break;
}tmp[tot] = ;
work();
for (int i = ; i < ans.size(); i++) printf("%c", ans[i] + 'a');
printf("\n");getchar();
}
return ;
}