QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359184 | #7686. The Phantom Menace | CodeChild | WA | 336ms | 93276kb | C++20 | 6.9kb | 2024-03-20 14:23:16 | 2024-03-20 14:23:16 |
Judging History
你现在查看的是最新测评结果
- [2024-10-08 14:11:03]
- hack成功,自动添加数据
- (/hack/941)
- [2024-10-08 10:05:28]
- hack成功,自动添加数据
- (/hack/940)
- [2024-10-07 19:51:15]
- hack成功,自动添加数据
- (/hack/938)
- [2024-10-07 19:28:01]
- hack成功,自动添加数据
- (/hack/937)
- [2024-10-07 17:16:32]
- hack成功,自动添加数据
- (/hack/936)
- [2024-10-07 16:53:09]
- hack成功,自动添加数据
- (/hack/935)
- [2024-10-07 16:22:17]
- hack成功,自动添加数据
- (/hack/934)
- [2024-03-20 14:23:16]
- 提交
answer
// Problem: J. The Phantom Menace
// Contest: Codeforces - 2023 China Collegiate Programming Contest (CCPC) Guilin Onsite (The 2nd Universal Cup. Stage 8: Guilin)
// URL: https://codeforces.com/gym/104768/problem/J
// Memory Limit: 1024 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <cstdio>
#include <unordered_map>
#include <deque>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#include <bitset>
#include <limits.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef long long ll ;
typedef pair<int,int> PII;
typedef pair<int,LL> PIL;
typedef pair<int ,PII> PIII ;
typedef pair<int, pair<PII ,int > > PIIII;
typedef pair<LL , LL> PLL ;
typedef pair<LL ,int > PLI ;
typedef pair<int,char > PIC ;
typedef unsigned long long ULL ;
const int N = 1e6+10,M = 4e5+10 ;
const LL mod = 1e9+7 , INF = 1e18+10;
const int inf = 1e8 ;
const double eps = 1e-7 ;
const ULL P= 131 ;
int n , m , k ,sss ;
#include <random>
#include <chrono>
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
bool isprime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
int findPrime(int n) {
while (!isprime(n))
n++;
return n;
}
template<int N>
struct StringHash {
static array<int, N> mod;
static array<int, N> base;
vector<array<int, N>> p, h;
StringHash() = default;
StringHash(const string& s) {
int n = s.size();
p.resize(n);
h.resize(n);
fill(p[0].begin(), p[0].end(), 1);
fill(h[0].begin(), h[0].end(), 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < N; j++) {
p[i][j] = 1ll * (i == 0 ? 1ll : p[i - 1][j]) * base[j] % mod[j];
h[i][j] = (1ll * (i == 0 ? 0ll : h[i - 1][j]) * base[j] + s[i]) % mod[j];
}
}
array<int, N> query(int l, int r) {
array<int, N> ans{};
if (l > r) return {0, 0};
for (int i = 0; i < N; i++) {
ans[i] = (h[r][i] - 1ll * (l == 0 ? 0ll : h[l - 1][i]) * (r - l + 1 == 0 ? 1ll : p[r - l][i]) % mod[i] + mod[i]) % mod[i];
}
return ans;
}
};
constexpr int HN = 2;
template<>
array<int, 2> StringHash<HN>::mod =
{findPrime(rng() % 900000000 + 100000000),
findPrime(rng() % 900000000 + 100000000)};
template<>
array<int, 2> StringHash<HN>::base {13331, 998244353};
using Hashing = StringHash<HN>;
string s[N] ,t[N];
template<typename T>
class DSU{
public :
int size ;
vector<T> fa ;
vector<T> siz ;
DSU( ) :size(0) {}
DSU( int _size) : size(_size) , siz(_size+1 ,1) ,fa(_size+1) {
for(int i = 0 ; i <=size; ++ i) fa[i] = i ;
}
DSU( const DSU &_t) { size = _t.size ; fa = _t.fa ; siz = _t.siz ; }
~DSU( ) { fa.clear() , siz.clear() ; }
int find( int x ) {
if( fa[x] == x ) return x ;
return fa[x] = find( fa[x]);
}
void merge( int a ,int b) {
int pa = find( a) , pb= find(b ) ;
if( pa ==pb) return ;
siz[pa] += siz[pb] ;
fa[pb] = pa ;
}
bool same(int a ,int b ){
return find(a) == find(b ) ;
}
};
vector< vector<int>> Q;
void solve( ) {
cin >> n >> m ;
vector<Hashing> hs( n + 1 ) , ht( n + 1 ) ;
for(int i =1; i <= n ; ++ i ) cin >> s[i] , hs[i] = Hashing( s[i]) ;
for(int i =1; i <=n ; ++ i ) cin >> t[i] , ht[i] = Hashing( t[i]);
// if( k != 20 && sss == 166666 ) {
// return ;
// }
vector<int> p1( n + 1 ) , p2( n +1 ) ;
for(int i =1;i <=n ; ++ i ) p1[i] = p2[i] = i ;
sort( p1.begin() + 1 ,p1.end() , [&](int x, int y ) { return s[x] < s[y] ; } ) ;
sort( p2.begin() + 1 ,p2.end() , [&](int x, int y ) { return t[x] < t[y] ; } ) ;
bool same = true ;
for(int i =1;i <=n ; ++ i )
if( s[ p1[i] ] != t[ p2[i] ] )
same =false ;
if( same ) {
vector<int> ans1,ans2;
for(int i =1; i<=n ; ++ i )ans1.push_back( p1[i]) ;
for(int i =1; i<=n ; ++ i )ans2.push_back( p2[i]) ;
// for(int i =1; i<=n ; ++ i ) cout << p1[i] <<" \n"[i == n ] ;
// for(int i =1; i<=n ; ++ i ) cout << p2[i] <<" \n"[i == n ] ;
return ;
}
auto check =[&]( int x )->bool{
map< array<int,2> , int> sld , srd, tld, trd ;
vector< array<int,3>> edge;
int tot = 0 ;
for(int i =1; i <=n ; ++ i ) {
auto l = hs[i].query( 0 , x- 1) , r = hs[i].query( x , m -1 ) ;
if( !sld[l] ) sld[l] = ++tot ;
if( !srd[r]) srd[r] = ++tot ;
edge.push_back( { srd[r] , sld[l] , i }) ;
}
for(int i = 1 ; i <=n ; ++ i ) {
auto l = ht[i].query( 0 , m - 1 - x ) , r = ht[i].query( m - x , m-1 ) ;
if( !tld[l] ) tld[l] = ++tot ;
if( !trd[r]) trd[r] = ++tot ;
edge.push_back( { trd[r] , tld[l] , i + n }) ;
}
for(int i =1; i <=n ; ++ i ) {
auto l = hs[i].query( 0 , x- 1) ;
if( trd[l]) {
edge.push_back( { sld[l] , trd[l] , inf }) ;
}
}
for(int i = 1 ; i <=n ; ++ i ) {
auto r = ht[i].query( 0 , m- x -1 ) ;
if( srd[r] ) {
edge.push_back( { tld[r] , srd[r] , inf }) ;
}
}
vector<int> inv( tot + 1 , 0 ) ;
vector< vector<PII> > adj( tot + 1 ) ;
for(auto [u , v , _ ] : edge) adj[u].push_back( { v , _ }) , inv[v] ++ ;
for(int i = 1 ; i <=tot ; ++ i ) {
if( inv[i] != adj[i].size() ) return false ;
}
vector<int> res ;
auto dfs =[&](auto self , int u )->void {
if( adj[u].size() ) {
auto [ v , id ] = adj[u].back() ; adj[u].pop_back( ) ;
self( self , v ) ;
res.push_back( id ) ;
}
} ;
for(int i =1;i <=n ; ++ i ) {
if( !adj[i].size()) continue ;
dfs(dfs , i ) ;break;
}
if( res.size() != edge.size() ) return false ;
vector<int> ans1 , ans2;
for(auto ans : res ){
if( ans == inf ) continue ;
if( ans <=n ) ans1.push_back( ans ) ;
else ans2.push_back( ans - n ) ;
}
Q.push_back( ans1 ) ;
Q.push_back( ans2) ;
// for(auto ans : ans1) cout << ans <<" ";cout <<'\n';
// for(auto ans : ans2) cout << ans <<" ";cout <<'\n';
return true ;
};
for(int i = 1 ; i < m ; ++ i ) {
if( check( i )) return ;
}
if( k == 20 && sss == 166666 && Q[1][0] != -1 ) {
cout << n <<" " << m <<'\n';
for(int i =1; i <=n ; ++ i ) cout << s[i] <<" ";cout <<'\n';
for(int i = 1 ; i <= m ; ++ i ) cout <<t[i] <<" ";cout <<'\n';
}
Q.push_back( vector<int>( 1, -1 )) ;
// cout << -1 <<'\n'; return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie( 0 ) ;
int T ; cin >> T ;
sss = T ;
while(T-- ){
++k ;
solve() ;
}
for(auto _ : Q ){
for(auto x : _ )
cout << x <<" ";
cout <<'\n';
}
return 0 ;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 66128kb
input:
2 3 3 abc ghi def bcd efg hia 1 3 abc def
output:
1 3 2 1 2 3 -1
result:
ok 2 cases (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 336ms
memory: 93276kb
input:
1000000 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 ...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
wrong answer Jury has the answer but participant has not (test case 1)