C 从句子中寻找最短的子段
眼前的问题是:打印包含给定k个单词的第一个最短子段,忽略特殊字符、数字和大小写。如果未找到子段,则应返回“未找到子段” 样本输入:C 从句子中寻找最短的子段,c,C,眼前的问题是:打印包含给定k个单词的第一个最短子段,忽略特殊字符、数字和大小写。如果未找到子段,则应返回“未找到子段” 样本输入: This is a test. This is a programming test. This is a programming test in any language. 4 this a test programming a programming test This 样本输出: This is a test. This is a programming
This is a test. This is a programming test. This is a programming test in any language.
4
this
a
test
programming
a programming test This
样本输出:
This is a test. This is a programming test. This is a programming test in any language.
4
this
a
test
programming
a programming test This
我的8个测试用例通过了,但有2个没有通过。
我不知道怎么回事
我也在寻找更多/更好的测试用例
我的代码在C中:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LENGTH 200000
int main() {
char S[MAX_STRING_LENGTH]; // original string
char *smallCapsStr,*tempStr; //temp strings having only blank space and small letters
int i=0 ; // used in for loop as inc variable
int start, tail,start2=0,tail2=0; // used in storing start and end index "for answer"
int total; // total words to find in substring
char **arr; // array to store words to find
char **temparr ; // temp arr to store words
int ptr[16];
char *temp2; // temp string
int z=0; // variable informing the end of string
int val=0; //counter var
int x=0; //variable informing the start of string
smallCapsStr=(char*)malloc(sizeof(char)*200000);
char c;
//geting string and converting into small caps
do {
c=getchar();
if((c>='A' && c <='Z')||(c>='a' && c<='z')){
S[i]=(char)c;
*(smallCapsStr + i)=tolower(c);
i++;
}
else{
if(S[i-1]!=' ' && i!=0){
S[i]=' ';
*(smallCapsStr + i)=' ';
i++;
}
}
} while (c != '\n');
S[i]='\0';
*(smallCapsStr + i) ='\0';
scanf("%d",&total);
arr = (char ** )malloc(sizeof(char*)*total);
temparr = (char ** )malloc(sizeof(char*)*total);
// getting words to find in arr and temparr
for(i=0;i<total;i++){
arr[i]=(char*)malloc(sizeof(char)*14);
temparr[i]=(char*)malloc(sizeof(char)*14);
scanf("%s",ptr);
strcpy(arr[i],ptr);
strcpy(temparr[i],ptr);
}
tempStr=smallCapsStr;
while(*(tempStr+1)!='\0'){
sscanf(tempStr,"%s",ptr);
for(i=0;i<total;i++){
if(strcmp(ptr,temparr[i]) == 0){
if(x==0){
start = strlen(S)-strlen(tempStr) ;
temp2 = tempStr+strlen(ptr)+1;
}
x++;
temparr[i]="";
break;
}
}
// updating temp string
tempStr=tempStr+strlen(ptr)+1;
//if all words are found
if(x == total){
tail = strlen(S)-strlen(tempStr) -1;
x=0;
val++;
if(z == 0 || ((tail2-start2)>(tail-start)) ){
start2 = start;
tail2 = tail;
}
z++;
for(i=0;i<total;i++){
temparr[i]= arr[i];
}
tempStr=temp2;
if(val==70)
break;
}
}
// putting ans
if(start2 ==0 && tail2 ==0){
printf("NO SUBSEGMENT FOUND");
}
else{
for(i=start2 ; i<=tail2;i++){
printf("%c",S[i]);
}
}
free(temparr);
free(arr);
free(smallCapsStr);
}
#包括
#包括
#包括
#定义最大字符串长度200000
int main(){
char S[MAX_STRING_LENGTH];//原始字符串
char*smallCapsStr,*tempStr;//只有空格和小写字母的临时字符串
int i=0;//在for循环中用作inc变量
int start,tail,start2=0,tail2=0;//用于存储开始和结束索引“for answer”
int total;//要在子字符串中查找的字总数
char**arr;//存储要查找的单词的数组
char**temparr;//存储单词的temp arr
int-ptr[16];
char*temp2;//临时字符串
int z=0;//通知字符串结尾的变量
int val=0;//计数器变量
int x=0;//通知字符串开头的变量
smallCapsStr=(char*)malloc(sizeof(char)*200000);
字符c;
//获取字符串并转换为小大写字母
做{
c=getchar();
如果((c>='A'&&c='A'&&cList)列出失败的测试用例。这将有助于缩小问题的范围。更好。列出规范。@UmNyobe-我自己的所有测试用例都通过了,但不知道为什么在amazon上。interviewstreets 2测试用例“6和7”没有通过。请尝试下面的输入…aksdhaksjhk^&*(123 c z 3 aksdhaksjhk aksdhaksjhk z
您的代码返回未找到子段。。。。