子字符串中字符的外观

时间:2019-02-06 01:25:39

标签: c algorithm binary-search binary-data

给出一个S字符串,编写一个程序,如果字符C出现在S的子字符串中,该程序以位置I开始,并且长度为T,则该程序正确响应。

进入
S字符串由字母数字字符组成,长度最大为1,000,000。随后是整数N,后跟N个三元组C,I,T。您可以假定1≤N≤10,000则0≤I<| S | 1≤T≤| S | -我。

退出
对于每个问题,如果该字符出现在指示的子字符串中,则该行的开头为1,否则为0。

我的想法是实施二进制搜索,但有资格进行二进制搜索的法官说,它超出了预期的时间,我不知道如何优化它以使法官接受我的算法。例如,一个条目可以是:

how are you
3
h 0 3
1
o 2 5
1
a 4 4
0 

我的代码:

#include <stdio.h>
#include <string.h>

int busqcadena(char cadena[], int a[],char x,int pos,int n);

int main()
{
    char cadena[1000000]; 
    char c;
    int t = 0, i; 
    int pos, lon, a[1000000], j = 0;
    do{
        fflush(stdin);
        c = getchar();
        cadena[i++] = c;
        a[j] = j;
        j++;
    } while (c != '\n');
    cadena[ i - 1 ] = '\0'; 

    scanf("%d",&t);
    for( int k = 0; k < t; k++){
    char pa; pos = 0; lon = 0;
    scanf("%s %d %d",&pa, &pos, &lon);
    int busq3 = busqcadena(cadena,a,pa,pos,lon);
    if( busq3 != -1){
        printf("1\n");
    }else{
        printf("0\n");
    } 
}
    return 0; 
}

int busqcadena(char cadena[], int arr[],char x,int pos,int n){
    int a = 0, b = n - 1;
    while( a <= b ){
        int k = (a+b)/2;
        if( cadena[k] == x && arr[k] == pos){
            return arr[k]; 
        }
        if( arr[k] > pos ) b = k - 1;
        else a = k + 1;
    }
    return -1;
}

1 个答案:

答案 0 :(得分:0)

由于只有固定数量的字符,因此可以从索引else if(action.equals("Load")) { Reporter.log(description+"|"+data); driver.get(data); if(!TestBase.browserName.equals("Chrome")) { driver.manage().window().maximize(); screenSize=driver.manage().window().getSize().toString(); System.out.println("My screensize is "+screenSize); } }else if(action.equals("RefreshPage")) { driver.navigate().refresh(); wait.until(ExpectedConditions.visibilityOfElementLocated( By.xpath("//span[contains(text(),'salesforce.com, inc. All rights reserved.')]"))); } else if(action.equals("InsertData")) { Reporter.log(description+"|"+data); moveToElement(elementReference, referenceValue); findElement(elementReference, referenceValue).click(); findElement(elementReference, referenceValue).clear(); if (description.toLowerCase().contains("request name")|| referenceValue.contains("reqNme")){ String customNum=getDate("requestName"); findElement(elementReference, referenceValue).sendKeys(data+customNum); System.out.println("Request Name is "+data+customNum); } else { findElement(elementReference, referenceValue).sendKeys(data); } } else if(action.equals("uploadFile")) { File file = new File(data); String filePath=file.getAbsolutePath(); System.out.println(filePath); findElement(elementReference, referenceValue).clear(); findElement(elementReference, referenceValue).sendKeys(filePath); } else if(action.equals("uploadImage")) { Reporter.log(description+"||"+data); uploadImage(elementReference, referenceValue, "Logo", new File(data)); } else if(action.equals("waitElVisibility")) { wait = new WebDriverWait(driver,Long.parseLong(data)); wait.until(ExpectedConditions.visibilityOf(findElement(elementReference, referenceValue))); } else if(action.equals("waitElInVisibility")) { wait = new WebDriverWait(driver,Long.parseLong(data)); wait.until(ExpectedConditions.invisibilityOfElementLocated( By.xpath(referenceValue))); } 开始计算每个字符的计数。完成此操作后,您只需检查该字符的计数是否已从0增加到I。伪代码如下

T