给定一个字符串数组。数组同时包含空字符串和非空字符串。所有非空字符串均按排序顺序。空字符串可以出现在非空字符串之间的任何位置。
例子:
Input : arr[] = {"for", "", "", "", "geeks",
"ide", "", "practice", "" ,
"", "quiz", "", ""};
str = "quiz"
Output : 10
The string "quiz" is present at index 10 in
given array.
一个简单的解决方案是线性搜索给定的STR在字符串数组。
更好的解决方案是进行修改后的二进制搜索。像普通的二进制搜索一样,我们将给定的str与中间字符串进行比较。如果中间字符串为空,则找到最接近的非空字符串x(通过线性搜索两侧)。一旦找到x,我们将进行标准的二进制搜索,即,将给定的str与x进行比较。如果str与x相同,则返回x的索引。如果str更大,则递归右半,否则递归左半。
以下是该想法的实现:
C++
// C++ program to find location of a str in
// an array of strings which is sorted and
// has empty strings between strings.
#include
using namespace std;
// Compare two string equals are not
int compareStrings(string str1, string str2)
{
int i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
return -1;
return (str1[i] < str2[i]);
}
// Main function to find string location
int searchStr(string arr[], string str, int first,
int last)
{
if (first > last)
return -1;
// Move mid to the middle
int mid = (last+first)/2;
// If mid is empty , find closet non-empty string
if (arr[mid].empty())
{
// If mid is empty, search in both sides of mid
// and find the closest non-empty string, and
// set mid accordingly.
int left = mid - 1;
int right = mid + 1;
while (true)
{
if (left < first && right > last)
return -1;
if (right<=last && !arr[right].empty())
{
mid = right;
break;
}
if (left>=first && !arr[left].empty())
{
mid = left;
break;
}
right++;
left--;
}
}
// If str is found at mid
if (compareStrings(str, arr[mid]) == 0)
return mid;
// If str is greater than mid
if (compareStrings(str, arr[mid]) < 0)
return searchStr(arr, str, mid+1, last);
// If str is smaller than mid
return searchStr(arr, str, first, mid-1);
}
// Driver Code
int main()
{
// Input arr of Strings.
string arr[] = {"for", "", "", "", "geeks", "ide", "",
"practice", "" , "", "quiz", "", ""};
// input Search String
string str = "quiz";
int n = sizeof(arr)/sizeof(arr[0]);
cout << searchStr(arr, str, 0, n-1);
return 0;
}
Java
// Java program to find location of a str in
// an array of strings which is sorted and
// has empty strings between strings.
import java.util.*;
class GFG
{
// Compare two string equals are not
static int compareStrings(String str1,
String str2)
{
int i = 0;
while (i < str1.length() - 1 &&
str1.charAt(i) == str2.charAt(i))
i++;
if (str1.charAt(i) > str2.charAt(i))
return -1;
if (str1.charAt(i) < str2.charAt(i))
return 1;
else
return 0;
}
// Main function to find string location
static int searchStr(String[] arr, String str,
int first, int last)
{
if (first > last)
return -1;
// Move mid to the middle
int mid = (last + first) / 2;
// If mid is empty,
// find closet non-empty string
if (arr[mid].isEmpty())
{
// If mid is empty, search in both sides of mid
// and find the closest non-empty string, and
// set mid accordingly.
int left = mid - 1;
int right = mid + 1;
while (true)
{
if (left < right && right > last)
return -1;
if (right <= last && !arr[right].isEmpty())
{
mid = right;
break;
}
if (left >= right && !arr[left].isEmpty())
{
mid = left;
break;
}
right++;
left--;
}
}
// If str is found at mid
if (compareStrings(str, arr[mid]) == 0)
return mid;
// If str is greater than mid
if (compareStrings(str, arr[mid]) < 0)
return searchStr(arr, str, mid + 1, last);
// If str is smaller than mid
return searchStr(arr, str, first, mid - 1);
}
// Driver Code
public static void main(String[] args)
{
// Input arr of Strings.
String[] arr = { "for", "", "", "", "geeks",
"ide", "", "practice", "",
"", "quiz", "", "" };
// input Search String
String str = "quiz";
int n = arr.length;
System.out.println(searchStr(arr, str, 0, n - 1));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 program to find the location of
# an str in an array of strings which is sorted
# and has empty strings between strings.
# Compare two string equals are not
def compareStrings(str1, str2):
i = 0
while i < len(str1) - 1 and str1[i] == str2[i]:
i += 1
if str1[i] > str2[i]:
return -1
return str1[i] < str2[i]
# Main function to find string location
def searchStr(arr, string, first, last):
if first > last:
return -1
# Move mid to the middle
mid = (last + first) // 2
# If mid is empty , find closet non-empty string
if len(arr[mid]) == 0:
# If mid is empty, search in both sides of mid
# and find the closest non-empty string, and
# set mid accordingly.
left, right = mid - 1, mid + 1
while True:
if left < first and right > last:
return -1
if right <= last and len(arr[right]) != 0:
mid = right
break
if left >= first and len(arr[left]) != 0:
mid = left
break
right += 1
left -= 1
# If str is found at mid
if compareStrings(string, arr[mid]) == 0:
return mid
# If str is greater than mid
if compareStrings(string, arr[mid]) < 0:
return searchStr(arr, string, mid+1, last)
# If str is smaller than mid
return searchStr(arr, string, first, mid-1)
# Driver Code
if __name__ == "__main__":
# Input arr of Strings.
arr = ["for", "", "", "", "geeks", "ide", "",
"practice", "" , "", "quiz", "", ""]
# input Search String
string = "quiz"
n = len(arr)
print(searchStr(arr, string, 0, n-1))
# This code is contributed by Rituraj Jain
输出:
10