結果

問題 No.263 Common Palindromes Extra
ユーザー krotonkroton
提出日時 2015-07-14 20:01:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,580 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 61,196 KB
実行使用メモリ 129,432 KB
最終ジャッジ日時 2023-09-22 15:45:10
合計ジャッジ時間 3,137 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 361 ms
125,144 KB
testcase_11 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;

struct Node {
	int cnt;
	Node* child[26];

	Node():cnt(0){
		memset(child, 0, sizeof(child));
	}

	void add(int n, int cnt){
		if(child[n] == NULL){
			child[n] = new Node;
		}
		child[n]->cnt += cnt;
	}
};

void myfree(Node* a){
	if(a == NULL)return;
	for(int i=0;i<26;i++)myfree(a->child[i]);
	delete a;
}

ll count(Node* a, Node* b){
	if(a == NULL || b == NULL){
		return 0;
	}
	ll res = a->cnt * b->cnt;
	for(int i=0;i<26;i++){
		res += count(a->child[i], b->child[i]);
	}
	return res;
}

Node* builder(const vector<int>& S, bool odd){
	const int N = S.size();
	
	Node *root = new Node;
	for(int i=0;i<N;){
		int j = i;
		while(j < N && S[j] == S[i])++j;

		Node *r = root;

		int l = odd ? 1 : 2;
		int len = j - i;
		for(;l<=len;l+=2){
			r->add(S[i], len-l+1);
			r = r->child[S[i]];
		}

		if(l == len){
			int s = i - 1;
			int t = j;
			while(s >= 0 && t < N && S[s] == S[t]){
				r->add(S[s], 1);
				r = r->child[S[s]];
				--s;
				++t;
			}
		}
		i = j;
	}

	return root;
}

vector<int> conv(const string &S){
	vector<int> res(S.size());
	for(int i=0;i<S.size();i++){
		res[i] = S[i] - 'A';
	}
	return res;
}

int main(){
	string S, T;
	cin >> S >> T;

	vector<int> vs = conv(S), vt = conv(T);
	
	Node *ns, *nt;
  	ll res = 0;

	ns = builder(vs, true);
	nt = builder(vt, true);
	res += count(ns, nt);
	myfree(ns);
	myfree(nt);

	ns = builder(vs, false);
	nt = builder(vt, false);
	res += count(ns, nt);
	myfree(ns);
	myfree(nt);

	cout << res << endl;
	return 0;
}
0