030 - Substring with Concatenation of All Words
2015-11-11 18:39
337 查看
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in
s that is a concatenation of each word in words exactly once and without any intervening characters.
For example, given:
s:
words:
You should return the indices:
(order does not matter).
这题目我也晕了,给定一个字符串,另外一组关键字,在字符串中找到所有这些关键字级联起来的串的起始位置,这些关键字可以随意顺序
这个问题处理起来很纠结,以后在寻找更简单的解决方法
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
void fun(char* start, char* s, char** words, int wordsSize, int *mark, int *ret, int* returnSize)
{
int marksize = 0;
int wlen = strlen(words[0]);
int wordstr = wlen * wordsSize;
int remain = wordsSize;
int i;
while (strlen(s) >= wordstr) {
char *move = s + marksize * wlen;
//remain = wordsSize - marksize;
while (remain) {
for (i = 0;i < wordsSize; i++) {
if (mark[i]) continue;
if (!strncmp(words[i], move, wlen)) {
mark[i] = 1;
marksize++;
remain--;
break;
}
}
if (i >= wordsSize) break;
move += wlen;
}
if (!remain) {
ret[(*returnSize)++] = s - start;
}
for (i = 0; i < wordsSize; i++) {
if (!strncmp(words[i], s, wlen)) {
mark[i] = 0;
remain++;
marksize--;
break;
}
}
for (i = 0; i < wordsSize; i++) mark[i] = 0;
s += 1;
remain = wordsSize;
marksize = 0;
}
}
int* findSubstring(char* s, char** words, int wordsSize, int* returnSize)
{
*returnSize = 0;
int *mark = (int *)calloc(sizeof(int), wordsSize);
int *ret = (int *)calloc(sizeof(int), 8192);
fun(s, s, words, wordsSize, mark, ret, returnSize);
return ret;
}
s that is a concatenation of each word in words exactly once and without any intervening characters.
For example, given:
s:
"barfoothefoobarman"
words:
["foo", "bar"]
You should return the indices:
[0,9].
(order does not matter).
这题目我也晕了,给定一个字符串,另外一组关键字,在字符串中找到所有这些关键字级联起来的串的起始位置,这些关键字可以随意顺序
这个问题处理起来很纠结,以后在寻找更简单的解决方法
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
void fun(char* start, char* s, char** words, int wordsSize, int *mark, int *ret, int* returnSize)
{
int marksize = 0;
int wlen = strlen(words[0]);
int wordstr = wlen * wordsSize;
int remain = wordsSize;
int i;
while (strlen(s) >= wordstr) {
char *move = s + marksize * wlen;
//remain = wordsSize - marksize;
while (remain) {
for (i = 0;i < wordsSize; i++) {
if (mark[i]) continue;
if (!strncmp(words[i], move, wlen)) {
mark[i] = 1;
marksize++;
remain--;
break;
}
}
if (i >= wordsSize) break;
move += wlen;
}
if (!remain) {
ret[(*returnSize)++] = s - start;
}
for (i = 0; i < wordsSize; i++) {
if (!strncmp(words[i], s, wlen)) {
mark[i] = 0;
remain++;
marksize--;
break;
}
}
for (i = 0; i < wordsSize; i++) mark[i] = 0;
s += 1;
remain = wordsSize;
marksize = 0;
}
}
int* findSubstring(char* s, char** words, int wordsSize, int* returnSize)
{
*returnSize = 0;
int *mark = (int *)calloc(sizeof(int), wordsSize);
int *ret = (int *)calloc(sizeof(int), 8192);
fun(s, s, words, wordsSize, mark, ret, returnSize);
return ret;
}
相关文章推荐
- Linux进程间通信(IPC)编程实践(四) 详解System V消息队列(2)(msgsnd & msgrcv)
- Android基于G-Sensor的计步算法
- Hadoop 上的HIVE,HBase和Pig Latin 数据流编程语言
- 029 - Divide Two Integers
- 我虽是一个编程的人,但是,我感觉自己能力还不行,简单介绍下自己吧
- 第11回 我猜小曹不会用Broadcast
- 关于测试人员的职业发展
- Codeforces Round #278 (Div. 1) A. Fight the Monster 暴力
- Thread系列——Thread.Sleep(0)
- 你不知道的JavaScript--Item32 DOM基础详解2
- Could not load the "*.png" image referenced from a nib in the bundle with identifier
- Linux GPIO驱动 - 驱动框架概述
- Ubuntu 14.04.3 LTS如何安装谷歌输入法
- Maven多环境配置实战 Filter
- Ubuntu 14.04.3 LTS如何安装谷歌输入法
- Ubuntu 14.04.3 LTS如何安装谷歌输入法
- 如何消除类型是submit类型的按钮的默认文字 ‘确认提交’
- Zabbix 实现微信短信告警
- cocos2d-x mruby学习笔记--扩展2(mruby code)
- 028 - Implement strStr()