QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#239934#7686. The Phantom Menaceucup-team197#WA 1436ms197016kbC++173.6kb2023-11-05 00:53:172023-11-05 00:53:17

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)
  • [2023-11-05 00:53:17]
  • 评测
  • 测评结果:WA
  • 用时:1436ms
  • 内存:197016kb
  • [2023-11-05 00:53:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
ll n,m;
string a[1000005],b[1000005];
const ll base=2977;
void add(ull &x,char c){
    x = (x * base + c);
}
pair<int,int>g[1000005],h[1000005];

unordered_map<ull, ll> mp,mp1,mp2;
bool check0(){
	mp.clear();
	int sz=0;
	
	for(int i=1; i<=n ;i++){
		ull hsh = 0;
		for(auto c:a[i]) add(hsh,c);
		if(mp[hsh]==0) mp[hsh]=++sz;
		g[i]={mp[hsh],i};
	}
	for(int i=1; i<=n ;i++){
		ull hsh = 0;
		for(auto c:b[i]) add(hsh,c);
		if(mp[hsh]==0) mp[hsh]=++sz;
		h[i]={mp[hsh],i};
	}
	sort(g+1,g+n+1);sort(h+1,h+n+1);
	for(int i=1; i<=n ;i++) if(g[i].fi!=h[i].fi) return false;
	for(int i=1; i<=n ;i++) cout << g[i].se << ' ';
	cout << '\n';
	for(int i=1; i<=n ;i++) cout << h[i].se << ' ';
	cout << '\n';
	return true;
}

const int N=4e6+5;
ull pwr[N];
vector<pair<int,int> >adj[N];
int in[N],out[N];
vector<int>ans;

vector<vector<ull>> a_hsh, b_hsh;

ull get_hash(const vector<ull> &hsh, int l, int r){
    ull sum = hsh[r];
    if(l) sum -= hsh[l - 1] * pwr[r - l + 1];
    return sum;
}

void dfs(int id){
	//if(adj[id].empty()) return;
	while(!adj[id].empty()){
		auto c=adj[id].back();adj[id].pop_back();
		dfs(c.se);
		ans.push_back(c.fi);
	}
}
void adde(int u,int v,int w){
	//cout << "Adde " << u << ' ' << v << ' ' << w << endl;
	adj[u].push_back({w,v});
	out[u]++;in[v]++;
}
bool check(int p){
	//cout << "Check " << p << endl;
	mp1.clear();mp2.clear();
	int sz=0;
	for(int i=1; i<=4*n ;i++){
		adj[i].clear();
		in[i]=0;out[i]=0;
	}
	for(int i=1; i<=n ;i++){
		ull hsh = get_hash(a_hsh[i], 0, p - 1);
		if(mp1[hsh]==0) mp1[hsh]=++sz;
		int v1=mp1[hsh];
		
		hsh = get_hash(a_hsh[i], p, m - 1);
		if(mp2[hsh]==0) mp2[hsh]=++sz;
		int v2=mp2[hsh];
		adde(v1,v2,i);
	}
	for(int i=1; i<=n ;i++){
		ull hsh = get_hash(b_hsh[i], m - p, m - 1);
		if(mp1[hsh]==0){
            return false;
        }
		int v1=mp1[hsh];
		
		hsh = get_hash(b_hsh[i], 0, m - p - 1);
		if(mp2[hsh]==0){
            return false;
        }
		int v2=mp2[hsh];
		adde(v2,v1,i+n);
	}
	for(int i=1; i<=sz ;i++) if(in[i]!=out[i]) return false;
	ans.clear();
	dfs(1);
	if(ans.size()!=2*n) return false;
	reverse(ans.begin(),ans.end());
	for(int i=0; i<2*n ;i+=2) cout << ans[i] << ' ';
	cout << '\n';
	for(int i=1; i<2*n ;i+=2) cout << ans[i]-n << ' ';
	cout << '\n';
	return true;
}

clock_t timer;
bool time_left(){
    return (((float)clock() - (float)timer) / (float)CLOCKS_PER_SEC) <= 3.9;
}

void solve(){
    timer = clock();
	cin >> n >> m;
	for(int i=1; i<=n ;i++) cin >> a[i];
	for(int i=1; i<=n ;i++) cin >> b[i];

    a_hsh.resize(n + 1, vector<ull>(m + 1));
    b_hsh.resize(n + 1, vector<ull>(m + 1));

    for(int i = 1; i <= n; ++i){
        ull curr = 0;
        for(int j = 0; j < m; ++j){
            curr *= base;
            curr += a[i][j];
            a_hsh[i][j] = curr;
        }

        curr = 0;
        for(int j = 0; j < m; ++j){
            curr *= base;
            curr += b[i][j];
            b_hsh[i][j] = curr;
        }
    }

    vector<int> permutation(m - 1);
    for(int j = 0; j < m - 1; ++j)
        permutation[j] = j + 1;

    mt19937 mt(3);
    shuffle(permutation.begin(), permutation.end(), mt);

	if(check(0)) return;
	for(int j: permutation){
        if(!time_left()) break;
		if(check(j)) return;
	}
	cout << "-1\n";
}
int main(){
    pwr[0] = 1;
    for(int i = 1; i < N; ++i)
        pwr[i] = pwr[i - 1] * base;

	ios::sync_with_stdio(false);cin.tie(0);
	int t;cin >> t;while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 21ms
memory: 197016kb

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: 1436ms
memory: 195876kb

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
-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)