枚举子类字符串
好吧,GCC/G++
strrev不是ANSI C/C++库函数,所以不支持
好吧。。额
Compiler Error
其实可以换成C++ STL来写
枚举枚举
STL 标准reverse算法
#include <fstream>
#include <string.h>
#include <cstdio>
using namespace std;
#define _cdebbug
//全局数据
char str[100][101];
int i,j,n;
int t;
int count = 0;
char ministr[101];
int ministrlen;
void getnext(char str[],int len,int next[])
{
int i = 0; //主串不回溯
int k = -1; //副串指针小标 一个i,一个k
next[0] = -1; //第一个模式next值为-1
while(i < len)
{
while(k >= 0 && str[i] != str[k])
k = next[k];
i++;
k++;
if (i == len)
break;
if (str[i] == str[k])
next[i] = next[k];
else
next[i] = k;
}
}
int kmp(char target[],char patter[],int next[],int tlen,int plen)
{
int i = 0;
int j = 0;
if (tlen < plen)
return -1;
while(i < plen && j < tlen)
{
if (i == -1 || target[j] == patter[i])
{
++i;
++j;
}
else
i = next[i];
}
if (i >= plen)
return(j - plen + 1);
else
return - 1;
}
int substrcnt(char * source)
{
int subStrLen = strlen(source);
int sourceStrLen = strlen(source);
bool found;
char subStr[101],revSubStr[101];
int i,j;
while(subStrLen > 0)
{
for (i = 0; i <= sourceStrLen - subStrLen; ++i)
{
strncpy(subStr, source + i, subStrLen);
strncpy(revSubStr, source + i, subStrLen);
subStr[subStrLen] = revSubStr[subStrLen] = '\0';
strrev(revSubStr);
found = true;
for (j = 0; j < n; ++j)
if (strstr(str[j], subStr) == NULL && strstr(str[j], revSubStr) == NULL)
{
found = false;
break;
}
if (found)
return subStrLen;
}
subStrLen--;
}
return 0;
}
int main()
{
//重定向
#ifdef _cdebbug
freopen("F:\\input.txt","r",stdin );
#endif
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ministrlen = 100;
for (i = 0; i < n; ++i)
{
scanf("%s",str[i]);
if (strlen(str[i]) < ministrlen)
{
strcpy(ministr,str[i]);
ministrlen = strlen(ministr);
}
}
count = substrcnt(ministr);
printf("%d\n",count);
}
/*
char strtest[] = "abcdaabcab";
int len = 10;
int next[10];
getnext(strtest,len,next);
int re = kmp(strtest,"aabcak",next,10,6);
*/
//解除重定向关闭文件
#ifdef _cdebbug
fclose(stdin);
#endif
return 0 ;
}