在USTCOJ的1324這道題中,當遇到tttttt tttttt tttttttttttt這樣的輸入實例時,如下僞代碼片段效率非常低。因爲有很多的重複計算在裏面。可通過存儲match函數調用結果減少重複計算。
int match(char *a, char *b, char *c)
{
if *c = 0
return 1
if *a = *b = *c
return match(a+1, b, c+1) || match(a, b+1, c+1)
if *a = *c
return match(a+1, b, c+1)
if *b = *c
return match(a, b+1, c+1)
return 0
}
其中代碼一是用數組來存儲結果,代碼二用stl裏面的set存儲參數對(pair)。代碼中while循環的目的是爲了減少遞歸層次,僅當不能判斷應該從哪一個子串(a或/和b)中取字符到主串(c)時,才跳出循環開始遞歸操作。
代碼一:
#include <cstdio>
#include <cstring>
#include <set>
#include <utility>
#define N 500
using namespace std;
char flag[N][N];
char stra[N], strb[N], strc[2*N];
int match(char *a, char *b, char *c)
{
while (*a || *b)
{
if(*c != *a && *c != *b)
return 0;
if(*a == *b)
break;
else
{
if (*a == *c)
a++, c++;
else
b++, c++;
}
}
//此時*a == *c,所以兩重遞歸
if (*a)
{
if (flag[a+1-stra][b-strb] == 0)
{
if (match(a+1, b, c+1))
return 1;
else
flag[a+1-stra][b-strb] = 1;
}
if (flag[a-stra][b+1-strb] == 0)
{
if (match(a, b+1, c+1))
return 1;
else
flag[a-stra][b+1-strb] = 1;
}
return 0;
}
//若*a == '\0',表示a、b字符串中的字符均已在c中匹配,故返回匹配成功
else
return 1;
}
int main()
{
freopen("1324.in", "r", stdin);
freopen("1324.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
memset(&flag[0][0], 0, N*N*sizeof(flag[0][0]));
scanf("%s%s%s", stra, strb, strc);
if (match(stra, strb, strc))
printf("Data set %d: yes\n", i);
else
printf("Data set %d: no\n", i);
}
return 0;
}
代碼二:
#include <cstdio>
#include <cstring>
#include <set>
#include <utility>
using namespace std;
typedef pair<char *, char *> MyType;
set<MyType> dpresult;
int match(char *a, char *b, char *c)
{
while (*a || *b)
{
if(*c != *a && *c != *b)
return 0;
if(*a == *b)
break;
else
{
if (*a == *c)
a++, c++;
else
b++, c++;
}
}
//此時*a == *c,所以兩重遞歸
if (*a)
{
MyType p(a+1, c+1);
if (dpresult.find(p) == dpresult.end())
{
if (match(a+1, b, c+1))
return 1;
else
dpresult.insert(p);
}
p.first = b+1;
if (dpresult.find(p) == dpresult.end())
{
if (match(a, b+1, c+1))
return 1;
else
dpresult.insert(p);
}
return 0;
}
//若*a == '\0',表示a、b字符串中的字符均已在c中匹配,故返回匹配成功
else
return 1;
}
int main()
{
freopen("1324.in", "r", stdin);
freopen("1324.out", "w", stdout);
char a[500], b[500], c[1000];
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
dpresult.clear();
scanf("%s%s%s", a, b, c);
if (match(a, b, c))
printf("Data set %d: yes\n", i);
else
printf("Data set %d: no\n", i);
}
return 0;
}