QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#122770#6567. Repetitive String Inventioncada_dia_mas_insanosTL 1ms3416kbC++172.6kb2023-07-11 03:07:072023-07-11 03:07:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-11 03:07:10]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3416kb
  • [2023-07-11 03:07:07]
  • 提交

answer

// Too many mind, no mind.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>

using namespace std;

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define COMP(x) sort(ALL(x)); x.resize(unique(ALL(x)) - (x).begin())
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

using pii = pair <int, int>;
using vi = vector <int>;
using vpi = vector <pii>;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using ld = long double;
using vld = vector<ld>;

const int maxn = 810, P=31; 
const ll mod[2] = {(ll)1e9+7, (ll)1e9+9};
ll add(ll a, ll b, int m) { return a + b >= mod[m] ? a + b - mod[m] : a + b; }
ll sub(ll a, ll b, int m) { return a >= b ? a - b : a - b + mod[m]; }
ll mul(ll a, ll b, int m) { return a*b%mod[m]; }
ll pre[maxn], su[maxn];
ll havep[maxn], haves[maxn];
ll pw[2][maxn];
ll lcp[maxn][maxn];
ll h[2][maxn];
int n;
char s[maxn];
void roll() {
	pw[0][0] = pw[1][0] = 1;
	fore(i, 1, maxn-1) {
		forn(j, 2) pw[j][i] = mul(pw[j][i-1], P, j);
	}
	forn(i, n) {
		forn(j, 2) {
			h[j][i] = mul(s[i] - 'a' + 1, pw[j][i], j);
			if (i) h[j][i] = add(h[j][i], h[j][i-1], j);
		}
	}
}
ll get(int l, int r) {
	vl hash(2);
	forn(j, 2) {
		hash[j] = h[j][r];
		if (l) hash[j] = sub(hash[j], h[j][l-1], j);
		hash[j] = mul(hash[j], pw[j][maxn-1-r], j);
	}
	return hash[0] << 32 | hash[1];
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> s;
	n = strlen(s);
	roll();
	int ans = 0;
	forn(i, n) fore(j, i, n-1) {
		ll cur = get(i, j);
		int len=j-i+1;

		for(int k=j+1; k+len-1 < n; k++) ans += get(k, k+len-1) == cur;

		//cout << "string: " << i << ' ' << j << ' ' << cur << endl;
		fore(w, 1, min({i, n-j-1})) {
			if (get(i-w, i-1) != get(j+1, j+w)) continue;

			for(int k=0; k +len-1< i-w; k++) ans += get(k, k+len-1) == cur;
			for(int k=j+w+1; k+len-1 < n; k++) ans += get(k, k+len-1) == cur;
			//cout << "here: " << w << endl;
		}

	}
	cout << ans << endl;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3344kb

input:

aaaa

output:

9

result:

ok single line: '9'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3416kb

input:

axabxbcxcdxd

output:

22

result:

ok single line: '22'

Test #3:

score: -100
Time Limit Exceeded

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:


result: