findStr
Posted ims-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了findStr相关的知识,希望对你有一定的参考价值。
目录
- 规则
- 目录结构
- src/main.cpp
- src/findstr.cpp
- include/findstr.h
- ft/TestCase.cpp
- ft/makefile
- ftbuild.sh
- mainbuild.sh
规则
在一个由大小写字母组成的矩阵中查找一个给定的字符串,这个字符串要求在矩阵中串成一个拐直角弯的相连序列,
形状像贪吃蛇一样,如果矩阵中存在这样的序列则查找成功。
目录结构
src/main.cpp
#include "../include/findstr.h"
int main(int argc, char* argv[])
{
if(argc !=3)
{
cout<<"argc should is 3, now it is "<<argc<<endl;
return 1;
}
findStr(argv[1],argv[2]);
return 0;
}
src/findstr.cpp
#include "../include/findstr.h"
using namespace std;
StrMatrix::StrMatrix(vector<string>&vectstr)
{
row = vectstr.size();
if(row>0)
{
col = vectstr[0].length();
}
else
return;
size = row*col;
pchar = new char[size];
pflag= new int[size];
int trow=0;
for (string str :vectstr)
{
for(int j=0; j<col ; j++)
pchar[j+trow*col] = str[j];
trow++;
}
}
void StrMatrix::clearFlag()
{
for (unsigned j = 0; j<size; j++)
{
pflag[j] = 1;
}
}
void StrMatrix::print()
{
for (unsigned j = 0; j<size; j++)
{
cout<<pchar[j]<<" ";
if((j+1)%col==0)
cout<<endl;
}
}
char StrMatrix::getchar(unsigned i, unsigned j)
{
if(i<row && j<col)
return pchar[i*col+j];
else
return ‘ ‘;
}
StrMatrix::~StrMatrix()
{
if (pchar != NULL)
{
delete[]pchar;
pchar= NULL;
}
if (pflag!= NULL)
{
delete[]pflag;
pflag= NULL;
}
}
void StrMatrix::setUsed(unsigned i,unsigned j)
{
if(i<row && j<col)
pflag[i*col+j]=0;
}
int StrMatrix::notUsed(unsigned i,unsigned j)
{
if(i<row && j<col)
return pflag[i*col+j] ;
else
return 0;
}
vector<string> split(const string &str, string ch = " ")
{
vector<string>ret;
int pos = 0;
int start = 0;
while ((pos = str.find(ch, start) )!= string::npos )
{
//cout << "pos:" << pos << endl;
if (pos>start)
ret.push_back(str.substr(start, pos-start));
start = pos+ch.size();
}
if (str.size()>start)
ret.push_back(str.substr(start));
return ret;
}
int checkPos(int i,int j, int*pos, char ch, int &validnum, StrMatrix &charMat)
{
if(charMat.notUsed(i,j) && charMat.getchar(i,j) == ch)
{
pos[validnum] = i;
pos[validnum+1] = j;
validnum+=2;
}
}
int recursion(int i,int j,StrMatrix &charMat, string curstr)
{
int ret=1;
int pos[6];
int validnum=0;
checkPos(i,j+1,pos,curstr[0],validnum,charMat);
checkPos(i,j-1,pos,curstr[0],validnum,charMat);
checkPos(i-1,j,pos,curstr[0],validnum,charMat);
checkPos(i+1,j,pos,curstr[0],validnum,charMat);
if(validnum>0 && curstr.length()==1)
{
//cout<<"has find!"<<endl<<endl;
return 0;
}
for(int cnt=0; cnt<validnum; cnt+=2)
{
//cout<<"ch: "<<charMat.getchar(pos[cnt],pos[cnt+1])<<endl;
charMat.setUsed(pos[cnt],pos[cnt+1]);
ret = recursion(pos[cnt],pos[cnt+1], charMat, curstr.substr(1));
if(ret == 0)
return 0;
}
return ret;
}
int findSingleStr(StrMatrix &charMat, string& curstr)
{
int ret=1;
for(int i=0; i<charMat.row; i++)
{
for(int j=0; j<charMat.col ; j++)
{
if(charMat.getchar(i,j) == curstr[0] )
{
if(curstr.length()==1)
return 0;
cout<<"head char:"<<charMat.getchar(i,j)<<endl;
charMat.clearFlag();
charMat.setUsed(i,j);
ret = recursion(i,j,charMat,curstr.substr(1));
if (ret == 0)
{
cout<<"find str:"<<curstr<<endl;
return 0;
}
}
}
}
return ret;
}
int findStr(string argvone,string argvtwo)
{
vector<string>onemat=split(argvone);
StrMatrix charMat(onemat);
charMat.print();
vector<string>strMatrix=split(argvtwo);
int ret=0;
for (string curstr :strMatrix)
{
//cout<<"curstr:"<<curstr<<endl;
ret |= findSingleStr(charMat, curstr );
}
return ret;
}
include/findstr.h
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class StrMatrix{
public:
unsigned row, col, size;
char*pchar;
int*pflag;
StrMatrix(vector<string>&vectstr);
~StrMatrix();
void setUsed(unsigned i,unsigned j);
int notUsed(unsigned i,unsigned j);
void clearFlag();
void assignCharArr(vector<string>&);
char getchar(unsigned i, unsigned j);
void print();
};
vector<string> split(const string &str, string ch );
int findSingleStr(StrMatrix &charMat, string& curstr);
int findStr(string argvone,string argvtwo);
ft/TestCase.cpp
#include <gtest/gtest.h>
#include "../include/findstr.h"
TEST(FooTest, SingleCharCase)
{
EXPECT_EQ(findStr("qwer asdf zxcv", "s a d"),0);
}
TEST(FooTest, multiStrCase)
{
EXPECT_EQ(findStr("qwer asdf zxcv", "qwsxcv qwedsazxcv rfvcxzaswq"),0);
}
TEST(FooTest, UseTwiceiSameChar)
{
EXPECT_EQ(findStr("qwer asdf zxcv", "aa cvc qazxz"),1);
}
TEST(FooTest, NotSerieschar)
{
EXPECT_EQ(findStr("qwer asdf zxcv", "qsc asf zxdf qwdf"),1);
}
ft/makefile
cc = g++ -std=c++11
prom = ../output/ft
deps = ../include/findstr.h
obj = ../src/findstr.o TestCase.o
LIBS = -lgtest -lgtest_main -lpthread
$(prom): $(obj)
$(cc) $(obj) $(LIBS) -o $(prom)
%.o: %.c $(deps)
$(cc) -c $< -o [email protected]
ftbuild.sh
cd ft
make
cd ../output
echo $(pwd)
./ft
mainbuild.sh
g++ -std=c++11 -g src/*.cpp -o output/findstr
以上是关于findStr的主要内容,如果未能解决你的问题,请参考以下文章