时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:1026

解决:571

题目描述:

给出一个01字符串(长度不超过100),求其每一个子串出现的次数。

输入:

输入包含多行,每行一个字符串。

输出:

对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。

样例输入:
10101
样例输出:
0 2
01 2
1 3
10 2
101 2
来源:
2010年北京大学计算机研究生机试真题

思路:

计数然后排序。呃,我怎么会写了这么长的代码。。。

代码:

#include <stdio.h>
#include <string.h> #define M 100 int main(void)
{
int n, i, j, k, m;
char s[M+1], son[M][M][M+1];
char *pson[M][M], *ptmp;
int c[M][M], count[M];; while (scanf("%s", s) != EOF)
{
//printf("s=%s\n", s);
for (i=0; i<M; i++)
{
for (j=0; j<M; j++)
c[i][j] = 0;
} n = strlen(s);
for (i=1; i<=n; i++)
{
for (j=0; j<=n-i; j++)
{
strncpy(son[i-1][j], s+j, i);
son[i-1][j][i] = '\0';
}
//for (j=0; j<=n-i; j++)
//{
// printf("i=%d, j=%d, s=%s\n", i, j, son[i-1][j]);
//}
count[i-1] = 0;
for (j=0; j<=n-i; j++)
{
ptmp = son[i-1][j];
if (j==0)
{
//printf("i=%d, j=%d, count[i-1]=%d, ptmp=%s\n", i, j, count[i-1], ptmp);
pson[i-1][j] = ptmp;
count[i-1] ++;
c[i-1][j] ++;
}
else
{
//printf("i=%d, j=%d, count[i-1]=%d, ptmp=%s\n", i, j, count[i-1], ptmp);
for (k=0; k<count[i-1]; k++)
{
/*
if (i==2 && k<2 )
{
printf("===\n");
for (int jj=0; jj<count[i-1]; jj++)
{
printf("i=%d, jj=%d, s=%s, c=%d\n", i, jj, pson[i-1][jj], c[i-1][jj]);
}
printf("===\n");
}
*/
if (strcmp(ptmp, pson[i-1][k]) == 0)
{
//printf("strcmp==0, i=%d, j=%d, k=%d, ptmp=%s, pson[i-1][k]=%s\n", i, j, k,
//ptmp, pson[i-1][k]);
c[i-1][k] ++;
break;
}
else if (strcmp(ptmp, pson[i-1][k]) < 0)
{
//printf("strcmp<0, i=%d, j=%d, k=%d, ptmp=%s, pson[i-1][k]=%s\n", i, j, k,
//ptmp, pson[i-1][k]);
for (m=count[i-1]-1; m>=k; m--)
{
pson[i-1][m+1] = pson[i-1][m];
c[i-1][m+1] = c[i-1][m];
}
pson[i-1][k] = ptmp;
c[i-1][k] = 1;
count[i-1] ++;
break;
}
//printf("strcmp>0, i=%d, j=%d, k=%d, ptmp=%s, pson[i-1][k]=%s\n", i, j, k,
//ptmp, pson[i-1][k]);
if (k == count[i-1]-1)
{
k ++;
pson[i-1][k] = ptmp;
c[i-1][k] = 1;
count[i-1] ++;
break;
}
}
}
//printf("-----i=%d, j=%d, pson[i-1][count[i-1]-1]=%s\n", i, j, pson[i-1][count[i-1]-1]);
}
/*
printf("===\n");
for (j=0; j<count[i-1]; j++)
{
printf("i=%d, j=%d, s=%s, c=%d\n", i, j, pson[i-1][j], c[i-1][j]);
}
printf("===\n");
*/
} int nk, nm;
int ctmp;
for (i=1; i<=n; i++)
{
for (j=0; j<count[i-1]; j++)
{
//printf("i=%d, j=%d\n", i, j);
if (i==n && j==count[i-1]-1)
break;
for (k=1; k<=n-i+1; k++)
{
for (m=0; m<count[k-1]; m++)
{
//printf("i=%d, j=%d, k=%d, m=%d\n", i, j, k, m);
if (k==n-i+1 && m>=count[k-1]-1-j)
break;
if (m==count[k-1]-1)
{
nk = k+1;
nm = 0;
}
else
{
nk = k;
nm = m+1;
}
if (strcmp(pson[k-1][m], pson[nk-1][nm]) > 0)
{
ptmp = pson[k-1][m];
pson[k-1][m] = pson[nk-1][nm];
pson[nk-1][nm] = ptmp;
ctmp = c[k-1][m];
c[k-1][m] = c[nk-1][nm];
c[nk-1][nm] = ctmp;
}
}
if (k==n-i+1 && m==count[k-1]-1-j)
break;
}
}
if (i==n && j==count[i-1]-1)
break;
} for (i=1; i<=n; i++)
{
//printf("\n");
for (j=0; j<count[i-1]; j++)
{
if (c[i-1][j] >= 2)
printf("%s %d\n", pson[i-1][j], c[i-1][j]);
}
//printf("\n");
}
} return 0;
}
/**************************************************************
Problem: 1149
User: liangrx06
Language: C
Result: Accepted
Time:400 ms
Memory:1952 kb
****************************************************************/

05-23 01:30