QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#180260 | #1777. Fortune From Folly | wint_x19 | WA | 0ms | 17928kb | C++23 | 10.5kb | 2023-09-15 17:21:44 | 2023-09-15 17:21:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
#define int long long
#define db long double
const double eps = 1e-6;
int mod = 1000000000000002493;
const int maxn = 550;
int m[maxn][maxn];
int ans[maxn];//解
int con;//0表示唯一解 1表示多解 2表示无解
int n;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void mul(int a, int p) {//第a行同乘p
for (int i = 1;i <= n + 1;i++) {
m[a][i] *= p;
}
}
void sub(int a, int x, int b, int y) {//第a行的x倍减去b行的y倍
for (int i = 1;i <= n + 1;i++) {
m[a][i] = m[a][i] * x - m[b][i] * y;
}
}
void exch(int a, int b) {//交换a,b两行
for (int i = 1;i <= n + 1;i++) {
m[a][i] ^= m[b][i];
m[b][i] ^= m[a][i];
m[a][i] ^= m[b][i];
}
}
void gauss() {//n阶矩阵,标准矩阵
for (int i = 1;i <= n;i++) {
if (m[i][i] == 0) {
for (int j = i + 1;j <= n;j++) {
if (m[j][i]) { exch(i, j); }
}
if (m[i][i] == 0) {
con = 1;
continue;
}
}
for (int j = 1;j <= n;j++) {
if (j != i) {
int s1 = m[i][i];
int s2 = m[j][i];
if (s2 == 0) continue;
else {
int g = gcd(s1, s2);
sub(j, s1 / g, i, s2 / g);
}
}
}
}
if (con == 0) {
for (int i = 1;i <= n;i++) {
ans[i] = m[i][n + 1] / m[i][i];
}
}
}
string s[maxn][maxn];
string name[maxn], AN[maxn];
int lll[maxn], l[maxn][maxn];
inline bool check1(int i, int j) {
if (j == 1) return true;
if (i != 1) {
for (int ii = 0; ii < lll[i]; ii++) {
if (s[i][1][ii] != s[i][j][ii]) return false;
}
}
for (int ii = 0; ii < lll[j]; ii++) {
if (s[j][1][ii] != s[i][j][ii + lll[i]]) return false;
}
return true;
}
inline bool check() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
if (l[i][j] != lll[i] + lll[j]) {
return false;
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
if (!check1(i, j)) {
return false;
}
}
}
}
return true;
}
bool run(int len) {
name[1] = s[1][2].substr(0, len);
lll[1] = len;
int flag = 1;
for (int i = 2; i <= n; i++) {
lll[i] = s[i][1].length() - lll[1];
if (lll[i] <= 0) {
flag = 0;
break;
}
}
if (flag == 0) return false;
if (check()) {
AN[1] = name[1];
for (int i = 2; i <= n; i++) {
for (int j = 0; j < lll[i]; j++) {
AN[i] += s[i][1][j];
}
}
return true;
}
else {
return false;
}
}
ull h[1000010], base[1000010], h1[1000010], base1[1000010], h2[1000010], base2[1000010], h12[1000010], base12[1000010];
ull query(int l, int r)//获取字符串[l,r]的哈希值
{
return h[r] - h[l - 1] * base[r - l + 1];
}
ull query1(int l, int r)//获取字符串[l,r]的哈希值
{
return h1[r] - h1[l - 1] * base1[r - l + 1];
}
ull query2(int l, int r)//获取字符串[l,r]的哈希值
{
return h2[r] - h2[l - 1] * base2[r - l + 1];
}
ull query12(int l, int r)//获取字符串[l,r]的哈希值
{
return h12[r] - h12[l - 1] * base12[r - l + 1];
}
void init(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base[0] = 1;
for (int i = 1;i <= n;i++)
{
h[i] = h[i - 1] * 233 + s[i];
base[i] = base[i - 1] * 233;// base[i]=M^i
}
}
void init1(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base1[0] = 1;
for (int i = 1;i <= n;i++)
{
h1[i] = h1[i - 1] * 233 + s[i];
base1[i] = base1[i - 1] * 233;// base[i]=M^i
}
}
void init2(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base2[0] = 1;
for (int i = 1;i <= n;i++)
{
h2[i] = h2[i - 1] * 131 + s[i];
base2[i] = base2[i - 1] * 131;// base[i]=M^i
}
}
void init12(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base12[0] = 1;
for (int i = 1;i <= n;i++)
{
h12[i] = h12[i - 1] * 131 + s[i];
base12[i] = base12[i - 1] * 131;// base[i]=M^i
}
}
ull h4[1000010], base4[1000010], h14[1000010], base14[1000010], h24[1000010], base24[1000010], h124[1000010], base124[1000010];
ull query4(int l, int r)//获取字符串[l,r]的哈希值
{
return h4[r] - h4[l - 1] * base4[r - l + 1];
}
ull query14(int l, int r)//获取字符串[l,r]的哈希值
{
return h14[r] - h14[l - 1] * base14[r - l + 1];
}
ull query24(int l, int r)//获取字符串[l,r]的哈希值
{
return h24[r] - h24[l - 1] * base24[r - l + 1];
}
ull query124(int l, int r)//获取字符串[l,r]的哈希值
{
return h124[r] - h124[l - 1] * base124[r - l + 1];
}
void init4(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base4[0] = 1;
for (int i = 1;i <= n;i++)
{
h4[i] = h4[i - 1] * 13331 + s[i];
base4[i] = base4[i - 1] * 13331;// base[i]=M^i
}
}
void init14(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base14[0] = 1;
for (int i = 1;i <= n;i++)
{
h14[i] = h14[i - 1] * 13331 + s[i];
base14[i] = base14[i - 1] * 13331;// base[i]=M^i
}
}
void init24(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base24[0] = 1;
for (int i = 1;i <= n;i++)
{
h24[i] = h24[i - 1] * 2333 + s[i];
base24[i] = base24[i - 1] * 2333;// base[i]=M^i
}
}
void init124(string s)//初始化哈希
{
int n = s.size();
s = "0" + s;//让其下标从1开始
base124[0] = 1;
for (int i = 1;i <= n;i++)
{
h124[i] = h124[i - 1] * 2333 + s[i];
base124[i] = base124[i - 1] * 2333;// base[i]=M^i
}
}
void check2() {
string s1 = s[1][2];
string s2 = s[2][1];
int cnt1[33] = {0}, cnt2[33] = {0};
for (int i = 0; i < s1.length(); i++) {
cnt1[s1[i] - 'a']++;
}
for (int i = 0; i < s2.length(); i++) {
cnt2[s2[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt1[i] != cnt2[i]) {
cout << "NONE\n";
return ;
}
}
if(s1.length() != s2.length() || s1.length() == 1) {
cout << "NONE\n";
return;
}
int len = s1.length();
init(s1); init1(s2);init2(s1);init12(s2);
init4(s1); init14(s2);init24(s1);init124(s2);
int cnt = 0, pos;
for(int i = 0; i < s1.length() - 1; i++) {
// if(query(1, i + 1) == query1(len - i, len) && query2(1, i + 1) == query12(len - i, len) && query4(1, i + 1) == query14(len - i, len) && query24(1, i + 1) == query124(len - i, len)){
// cnt++; pos = i + 1;
// }
// if(cnt == 2) break;
if(query(1, i + 1) == query1(len - i, len) && query(i + 2, len) == query1(1, len - i - 1)
&& query2(1, i + 1) == query12(len - i, len) && query2(i + 2, len) == query12(1, len - i - 1)
&& query4(1, i + 1) == query14(len - i, len) && query4(i + 2, len) == query14(1, len - i - 1)
&& query24(1, i + 1) == query124(len - i, len) && query24(i + 2, len) == query124(1, len - i - 1)){
cnt++; pos = i + 1;
}
if(cnt == 2) break;
}
if(cnt == 0) {
cout << "NONE\n";
}
else if(cnt == 2) {
cout << "MANY\n";
}
else {
cout << "UNIQUE\n";
for(int i = 0; i < pos; i++) {
cout << s1[i];
}
cout << '\n';
for(int i = pos; i < s1.length(); i++) {
cout << s1[i];
}
cout << '\n';
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> s[i][j];
l[i][j] = s[i][j].length();
}
}
if (n == 2) {
int m = s[0][1].size();
string S = s[0][1] + "#" + s[1][0];
string T = s[1][0] + "#" + s[0][1];
int sz = S.size();
vector<int> fs(sz), ft(sz);
int p = 0;
for (int i = 1; i < sz; i++) {
while (p != 0 && S[p] != S[i])
p = fs[p - 1];
if (S[p] == S[i]) fs[i] = ++p;
}
p = 0;
for (int i = 1; i < sz; i++) {
while (p != 0 && T[p] != T[i])
p = ft[p - 1];
if (T[p] == T[i]) ft[i] = ++p;
}
string ansA, ansB;
bool found = false;
vector<bool> chk(sz);
for (int v = sz - 1; ; v = fs[v] - 1) {
if (fs[v] == 0) break;
chk[fs[v]] = true;
}
for (int v = sz - 1; ; v = ft[v] - 1) {
if (ft[v] == 0) break;
if (chk[m - ft[v]]) {
if (found) { cout << "MANY"; return ; }
else {
found = true;
ansA = S.substr(0, m - ft[v]);
ansB = T.substr(0, (int)ft[v]);
}
}
}
if (!found) {cout << "NONE"; return ;}
else {
cout << "UNIQUE\n";
cout << ansA << '\n';
cout << ansB << '\n';
}
return ;
}
for (int i = 1; i <= n; i++) {
if (i == n) {
m[i][n + 1] = l[2][3];
m[i][2] = 1;
m[i][3] = 1;
}
else {
m[i][n + 1] = l[1][i + 1];
m[i][1] = 1;
m[i][i + 1] = 1;
}
}
gauss();
for (int i = 1; i <= n; i++) {
if (ans[i] <= 0) con = 2;
}
// cout << con << '\n';
if (con == 0) {
if (run(ans[1])) {
cout << "UNIQUE\n";
for (int i = 1; i <= n; i++) {
cout << AN[i] << '\n';
}
}
else {
cout << "NONE\n";
}
}
else if (con == 2) {
cout << "NONE\n";
}
}
signed main() {
io;
int T = 1;
//cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 17928kb
input:
2 1 0.0006
output:
NONE
result:
wrong output format Expected double, but "NONE" found