#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX 1000
//计算字符串出现次数
int Findkey(char nstr[], char find[], int l1) {
int key = 0, i, j;
key = 0;
int l2 = strlen(find);
for ( i = 0; i <= l1 - l2; i++) {
for ( j = 0; j < l2; j++) {
if (nstr[j + i] != find[j]) break;
}
if (j == l2) key++;
}
return key;
}
int main() {
char nstr[MAX],
find[MAX][MAX]; //nstr:输入的字符串 find为遍历出来的所有字符串
int key = 0, l1, l2, i, j, m, times = 0, k = 0, kstr = 0;
scanf("%s", nstr);
l1 = strlen(nstr);
//遍历所有字符串
for ( i = 0; i < l1; i++) {
for (m = 1; m <= l1 - i; m++) {
k = 0;
for (j = i; j < m + i; j++) {
find[kstr][k] = nstr[j];
k++;
}
times ++;
kstr++;
}
}
//去掉重复字符串
for (i = 0; i < kstr; i++) {
for (int j = i + 1; j < kstr; j++) {
if (strcmp(find[i], find[j]) == 0) find[j][0] = '\0';
}
}
char findend[MAX][MAX],
findtmp[MAX][MAX]; // findend为最终去重和排序之后的字符串数组,findtemp为find去重之后的数组
int index[MAX], timesend = 0;
for (i = 0; i < times; i++) {//去掉空位。让字符串相邻排列,,因为前面去重后会出现空位
if (find[i][0] != '\0') {
strcpy(findtmp[timesend], find[i]);
timesend ++;
}
}
for ( i = 0; i < timesend; i++) {//排序,index[i]记录findtmp[i]里的字符串的排序大小
for (j = 0; j < timesend; j++) {
if (strcmp(findtmp[i], findtmp[j]) > 0) index[i]++;
else if (strcmp(findtmp[i], findtmp[j]) < 0) index[j]++;
}
}
int lengthend = 0;
for (i = 0; i < timesend; i++) { // 根据index里的信息 将findtmp里的字符串按从小到大排序到findend中
strcpy(findend[index[i]], findtmp[i]);
if (index[i] > lengthend) lengthend = index[i];
}
for (i = 0; i < lengthend; i++) {//把findend中的字符串送到Findkey中查找每一个字符串出现的次数 key
if (findend[i][0] != '\0') {
key = Findkey(nstr, findend[i], l1);
if (key > 1) {
printf("%s %d\n", findend[i], key);
}
}
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct TreeNode {
int count;
char val; // 0-左 1-右
struct TreeNode* left; //左节点-0
struct TreeNode* right; //右节点-1
} TNode;
void printTree(char* t, TNode* root) {
//前序遍历打印二叉树
if (root) {
if (root->count > 1) { //输出t中的所有字符,并且打印本次遍历的字符
printf("%s%c %d\n", t, root->val, root->count);
}
int length = 0;
for (int i = 0; t[i] != '\0'; i++) {
length++;
}
t[length] = root->val;
printTree(t, root->left);
printTree(t, root->right);
t[length] = '\0'; //剪枝
}
}
void getSubParam(char s[]) {
//构建一颗二叉树,字典树
TNode* initNode = (TNode*)malloc(sizeof(TNode)); //创世块
int length = 0;
while (s[length] != '\0') {
length++;
}
for (int i = 0; i < length; i++) {
TNode* tmp = initNode;
for (int j = i; j < length; j++) {
//双重循环
if (s[j] == '0') {
if (!tmp->left) {
//没有左孩子,手动申请一个空间放左孩子
tmp->left = (TNode*)malloc(sizeof(TNode));
tmp->left->val = '0';
tmp->left->count = 0;
}
tmp->left->count++;
tmp = tmp->left;
}
if (s[j] == '1') {
if (!tmp->right) {
tmp->right = (TNode*)malloc(sizeof(TNode));
tmp->right->val = '1';
tmp->right->count = 0;
}
tmp->right->count++;
tmp = tmp->right;
}
}
}
char* tmp1 = (char*)malloc(sizeof(char) * 101);
char* tmp2 = (char*)malloc(sizeof(char) * 101);
printTree(tmp1, initNode->left);
printTree(tmp2, initNode->right);
}
//字典二叉树
int main() {
char s[100];
// FILE* fp = fopen("./data.in", "r");
while (scanf("%s\n", s) != EOF) {
getSubParam(s);
}
}