محدودیت زمان: ۲ ثانیه
محدودیت حافظه: ۲۵۶ مگابایت
سرانجام هریپاتر توانست طلسم شکست دادن دشمن بزرگ خود مالفوی را ابداع کند! هری یک جفت دستبند قدرت دایرهای ساخته که روی مچ دستهای راست و چپش بسته میشود. او روی هر دستبند دنبالهای از حروف جادویی نوشته است که هر حرف فعال قدرت او را تقریبا به اندازه یک بید کتکزن افزایش میدهد! با این حال یک مشکل وجود دارد. دستبندها فقط زمانی کار میکنند که زیردنباله حروف فعال شده روی هر دو دستبند یکسان باشد. برای مثال در شکل زیر یک جفت دستبند داده شده که حروف دو رشته
a
b
a
b
d
c
b
c
c
ababdcbcc و
a
a
b
d
c
c
c
c
b
d
aabdccccbd به عنوان حروف جادویی روی آنها قرار گرفته است که یک فعالسازی بهینه از این حروف به هری قدرتی به اندازه ۱۴ بید کتکزن میدهد.
توضیح تصویر
روی دستبند اول حروف
b
a
b
d
c
c
c
babdccc در جهت ساعتگرد و روی دستبند دوم همین حروف در جهت پادساعتگرد فعال شده است. بطور کلی ترتیب حروف مهم است اما جهت زیر دنباله فعال روی هر دستبند (در جهت عقربههای ساعت یا خلاف جهت) ممکن است یکسان باشد یا نباشد. فراموش نکنید که دستبندها دایرهای هستند! شما باید به هری کمک کنید که تصمیم بگیرد چه زیردنباله بهینهای از حروف روی دستبندهایش را لازم است فعال کند تا بتواند مالفوی را شکست دهد.
ورودی
فایل ورودی حداکثر شامل ۱۰۰ نمونه ورودی است. (حداکثر شامل ۵ نمونه بزرگ) هر نمونه یک خط شامل یک جفت رشته که با فاصله جدا شده اند و مربوط به همان دنباله حروف روی دستبندهای قدرت چپ و راست هری است (به ترتیب) می باشد و هر رشته تنها از حروف کوچک تشکیل خواهد شد. طول هر رشته ورودی بین
1
1 تا
100
100 کاراکتر است به جز در نمونه های بزرگ که طول هر رشته ورودی بین
1
1 تا
1500
1500 کاراکتر می باشد.
خروجی
حداکثر قدرتی که هری با فعال کردن حروف روی دستبندها می تواند برسد (بر حسب واحد بید کتکزن) را چاپ کنید.
مثال
ورودی نمونه ۱
ababdcbcc aabdccccbd
harrypotter plorppothaa
potterharry plorppothaa
Plain text
خروجی نمونه ۱
14
12
12 write code in java17
Assistant
To solve this problem, we need to find the maximum length of a common substring that can appear in both bracelets. Since the bracelets are circular and can be read in both clockwise and counterclockwise directions, we have to consider all possible rotations and directions for both bracelets.
Approach:
Circular Nature Handling: Since the bracelets are circular, one way to handle rotations is to concatenate each bracelet with itself. This allows us to consider all possible rotations as linear substrings within the concatenated string.
Direction Handling: We need to consider both the original and reversed directions for each bracelet. So, for each bracelet, we will consider:
The original string concatenated with itself.
The reversed string concatenated with itself.
Longest Common Substring: For every pair of bracelets, we aim to find the longest common substring between:
The concatenated version of the first bracelet and both versions (original and reversed) of the second bracelet.
The concatenated version of the first reversed bracelet and both versions (original and reversed) of the second bracelet.
Binary Search with Rolling Hash: To efficiently find the longest common substring, we can use a binary search approach combined with a rolling hash technique. The binary search will help us narrow down the possible lengths of the common substring, while the rolling hash will allow us to quickly check if a substring of a given length exists in both bracelets.
Hashing: To reduce the chances of hash collisions, we use double hashing with two different prime moduli.
Implementation Details:
Preprocessing: For each bracelet, compute both the concatenated and reversed concatenated versions.
Binary Search: Start with the entire possible range of substring lengths and use binary search to find the maximum valid length.
Hash Calculation: Precompute hash values and powers for each bracelet string to allow O(1) hash calculation for any substring.
Code:
Below is the Java implementation of the solution based on the above approach:
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Solution {
static class RollingHash {
long[] hash1, hash2;
long[] power1, power2;
int n;
static final long MOD1 = 1000000007;
static final long MOD2 = 1000000009;
static final long BASE = 911382629;
public RollingHash(String s) {
n = s.length();
hash1 = new long[n + 1];
hash2 = new long[n + 1];
power1 = new long[n + 1];
power2 = new long[n + 1];
power1[0] = 1;
power2[0] = 1;
for (int i = 0; i < n; i++) {
hash1[i + 1] = (hash1[i] * BASE + s.charAt(i)) % MOD1;
hash2[i + 1] = (hash2[i] * BASE + s.charAt(i)) % MOD2;
power1[i + 1] = (power1[i] * BASE) % MOD1;
power2[i + 1] = (power2[i] * BASE) % MOD2;
}
}
// Get hash from l to r-1
public long getHash1(int l, int r) {
return (hash1[r] - hash1[l] * power1[r - l] % MOD1 + MOD1) % MOD1;
}
public long getHash2(int l, int r) {
return (hash2[r] - hash2[l] * power2[r - l] % MOD2 + MOD2) % MOD2;
}
public long getCombinedHash(int l, int r) {
return getHash1(l, r) * MOD2 + getHash2(l, r);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null){
if(line.trim().isEmpty()) continue;
String[] parts = line.trim().split(" ");
if(parts.length !=2 ){
continue;
}
String S = parts[0];
String T = parts[1];
int maxLen = solve(S, T);
sb.append(maxLen * 2).append("\n");
}
System.out.print(sb);
}
static int solve(String S, String T){
int n = S.length();
int m = T.length();
int maxPossible = Math.min(n, m);
// Prepare all possible rotations and directions
String S_doubled = S + S;
String S_rev = new StringBuilder(S).reverse().toString();
String S_rev_doubled = S_rev + S_rev;
String T_doubled = T + T;
String T_rev = new StringBuilder(T).reverse().toString();
String T_rev_doubled = T_rev + T_rev;
// Create RollingHash objects
RollingHash rhS = new RollingHash(S_doubled);
RollingHash rhS_rev = new RollingHash(S_rev_doubled);
RollingHash rhT = new RollingHash(T_doubled);
RollingHash rhT_rev = new RollingHash(T_rev_doubled);
int left =1, right = maxPossible, res=0;
while(left <= right){
int mid = left + (right - left)/2;
if(hasCommon(rhS, S_doubled, mid, n) && (hasAnyCommon(rhT, T_doubled, rhT_rev, T_rev_doubled, mid, m))){
res = mid;
left = mid +1;
}
else{
right = mid -1;
}
}
// Alternative approach to check for any possible rotation and direction
// Instead of changing direction of S, we keep S in one direction and change T
// To ensure we cover all possibilities, iterate through S and T directions
// Use binary search to find the maximum L
// Implement a helper function
res = binarySearchMax(S_doubled, rhS, T_doubled, rhT, T_rev_doubled, rhT_rev, Math.min(n,m));
return res * 2;
}
static int binarySearchMax(String S_doubled, RollingHash rhS, String T_doubled, RollingHash rhT, String T_rev_doubled, RollingHash rhT_rev, int maxPossible){
int left =1, right = maxPossible, res=0;
while(left <= right){
int mid = left + (right - left)/2;
// Get all hashes of S_doubled substrings of length mid
HashSet<Long> hashesS = getAllHashes(rhS, S_doubled.length(), mid);
// Get all hashes of T_doubled and T_rev_doubled
HashSet<Long> hashesT = getAllHashes(rhT, T_doubled.length(), mid);
HashSet<Long> hashesT_rev = getAllHashes(rhT_rev, T_rev_doubled.length(), mid);
boolean found = false;
for(Long h : hashesS){
if(hashesT.contains(h) || hashesT_rev.contains(h)){
found = true;
break;
}
}
if(found){
res = mid;
left = mid +1;
}
else{
right = mid -1;
}
}
return res;
}
static HashSet<Long> getAllHashes(RollingHash rh, int totalLen, int L){
HashSet<Long> set = new HashSet<>();
if(L > totalLen) return set;
for(int i=0; i <= totalLen - L; i++){
long h = rh.getCombinedHash(i, i+L);
set.add(h);
}
return set;
}
static boolean hasCommon(RollingHash rhS, String S_doubled, int L, int n){
if(L > S_doubled.length()) return false;
HashSet<Long> set = new HashSet<>();
for(int i=0; i <= S_doubled.length() - L && i < n; i++){
long h = rhS.getCombinedHash(i, i+L);
set.add(h);
}
return !set.isEmpty();
}
static boolean hasAnyCommon(RollingHash rhT, String T_doubled, RollingHash rhT_rev, String T_rev_doubled, int L, int m){
if(L > T_doubled.length()) return false;
HashSet<Long> set = new HashSet<>();
for(int i=0; i <= T_doubled.length() - L && i < m; i++){
long h = rhT.getCombinedHash(i, i+L);
set.add(h);
}
for(int i=0; i <= T_rev_doubled.length() - L && i < m; i++){
long h = rhT_rev.getCombinedHash(i, i+L);
set.add(h);
}
return !set.isEmpty();
}
}
Explanation:
RollingHash Class: This class precomputes the hash values and power values for a given string using two different moduli to minimize hash collisions.
Main Function: Reads input pairs of bracelet strings, processes each pair to find the maximum common substring length using the solve function, and then prints twice that length (since each character contributes to both bracelets).
Solve Function:
It first handles the circular nature by concatenating each bracelet with itself.
It also handles direction by considering both the original and reversed concatenated strings.
It uses a binary search approach to determine the maximum possible length of the common substring that exists in both bracelets.
Binary Search and Hash Comparison: The binarySearchMax function performs a binary search over possible substring lengths and uses hash sets to efficiently check for the existence of common substrings of a given length.
Final Output: The maximum length found is multiplied by 2 to account for contributions from both bracelets.