char a[num1],b[num2]拷贝用strcpy()

本文探讨了在底层开发中,由于效率考虑避免使用string,转而采用char数组进行字符串操作的方法。通过实例演示了如何从一个char数组复制内容到另一个char数组,强调了使用strcpy函数的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天,涉及到了底层开发,不能用string,效率不高。

于是char a[num1],char b[num2],

从b拷贝到a,

需要strcpy(a,b)即可。

#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<time.h> #define max2(a,b) ((a)>(b)?(a):(b)) int **Dp, MaxDP=3300; int min3(int a, int b, int c) { int min = a < b ? a : b; return min < c ? min : c; } int initDP() { int i; Dp = (int **)malloc(MaxDP*sizeof(int *)); for(i=0; i<MaxDP; i++) Dp[i] = (int *)malloc(MaxDP*sizeof(int)); return 0; } int error2(char *s) { printf("%s\n",s); exit(-1); } int editdistDP(char *str1, char *str2) { int i,j; int len1, len2; static int flag=0; (flag++) ? 1 : initDP(); len1 = strlen(str1)+1; len2 = strlen(str2)+1; (max2(len1,len2)>=MaxDP) ? error2("DP memory error!") : len1; for (i=0; i<=len1; i++) { for (j=0; j<=len2; j++) { if (i==0) Dp[i][j] = j; else if (j==0) Dp[i][j] = i; else if (str1[i-1] == str2[j-1]) Dp[i][j] = Dp[i-1][j-1]; else Dp[i][j] = 1 + min3(Dp[i][j-1], Dp[i-1][j], Dp[i-1][j-1]); } } return Dp[len1][len2]; } struct two_gram { char s1[38]; char s2[38]; }; int GramTwoSize; typedef struct two_gram_node { char s1[38]; char s2[38]; struct two_gram_node *left; struct two_gram_node *right; }Bst_Node; typedef Bst_Node* PtrToBst_Node; struct two_gram wrongWord[10000000]; int wrongWordSize; Bst_Node* wrongWordRoot = NULL; struct node { int isEnd; char alpha; struct node *son[29]; }; typedef struct node* TrieNodePtr; TrieNodePtr root; PtrToBst_Node bst_root; void InsertTireTree(char s[]); int FindWord(char s[]); int InsertBst_Node(Bst_Node* ROOT,char s1[],char s2[]); int FindBstTree(char s1[],char s2[]); void printBstTree(char s1[], char s2[], int Dp); char passage[10000000]={0}; int len_passage; void read_Word(char s[]); int cmp(const void *a,const void *b); int main() { root = (TrieNodePtr)calloc(1, sizeof(struct node)); bst_root = (PtrToBst_Node)calloc(1, sizeof(Bst_Node)); wrongWordRoot = (PtrToBst_Node)calloc(1,sizeof(Bst_Node)); int i; for(i = 0;i<26;i++) { root->son[i] = NULL; } initDP(); FILE *dict = fopen("dict.txt", "r"); FILE *input = fopen("in.txt", "r"); char buff[38]; while(fgets(buff,38,dict) != NULL) { buff[(strlen(buff)-1)] = '\0'; InsertTireTree(buff); } char ch = 0; while((ch = fgetc(input)) != EOF) { passage[len_passage++] = ch; } read_Word(passage); for(i=0;i<wrongWordSize;i++) { int Dp = FindBstTree(wrongWord[i].s1, wrongWord[i].s2); if(Dp==-1) { printf("%s: %s -> No suggestion",wrongWord[i].s1,wrongWord[i].s2); } else { printf("%s: %s -> ",wrongWord[i].s1,wrongWord[i].s2); printBstTree(wrongWord[i].s1, wrongWord[i].s2, Dp); } printf("\n"); } fclose(dict); fclose(input); return 0; } void InsertTireTree(char s[]) { TrieNodePtr P = root; int i=0; while(s[i]) { if(!isalpha(s[i])) continue; char ch = tolower(s[i]); int index = ch - 'a'; if (!P->son[index]) { P->son[index] = (TrieNodePtr)calloc(1, sizeof(struct node)); } i++; P = P->son[index]; } P->isEnd = 1; } int FindWord(char s[]) { TrieNodePtr P = root; int i=0; while(s[i]) { int index = tolower(s[i]) - 'a'; if(P->son[index] == NULL) return 0; P = P->son[index]; i++; } return P->isEnd; } void read_Word(char s[]) { int len = strlen(s); int flag = 1; char *word1 = (char *)malloc(38*sizeof(char)); char *word2 = (char *)malloc(38*sizeof(char)); char *temp = (char *)malloc(38*sizeof(char)); int gram = 0; int i=0,j=0; while(i < len) { s[i] = tolower(s[i]); if(flag == 1 &&s[i]!=' ' && s[i]!='\t'&& !isalpha(s[i])&& s[i+1]!='-') { gram = 0; } if(flag == 1 && isalpha(s[i])) { j = i; flag = 0; } if(flag == 0 && (i == len-1||!isalpha(s[i+1]))) { flag = 1; temp = word1; word1 = word2; word2 = temp; strncpy(word2, s+j, i - j + 1); word2[i - j + 1] = '\0'; if(gram < 2) { gram++; } if(gram == 2) { int find_word1 = FindWord(word1); int find_word2 = FindWord(word2); if(find_word1 && find_word2) { InsertBst_Node(bst_root, word1, word2); } if(find_word1 && !find_word2) { if(InsertBst_Node(wrongWordRoot, word1, word2)) { strcpy(wrongWord[wrongWordSize].s1,word1); strcpy(wrongWord[wrongWordSize].s2,word2); wrongWordSize++; } } } if(i < len &&s[i+1]!=' ' && s[i+1]!='\t' && s[i+1]!='\''&& !isalpha(s[i+1])&& s[i+1]!='-') { gram = 0; } } i++; } } int InsertBst_Node(Bst_Node* ROOT,char s1[],char s2[]) { if(ROOT == NULL) { strcpy(ROOT->s1, s1); strcpy(ROOT->s2, s2); ROOT->left = NULL; ROOT->right = NULL; return 1; } Bst_Node *P = ROOT; while(1) { if(strcmp(s1, P->s1) < 0) { if(P->left != NULL) { P = P->left; continue; } Bst_Node *newNode = (Bst_Node*)malloc(sizeof(Bst_Node)); strcpy(newNode->s1, s1); strcpy(newNode->s2, s2); newNode->left = NULL; newNode->right = NULL; P->left = newNode; return 1; } else if(strcmp(s1,P->s1) > 0) { if(P->right != NULL) { P = P->right; continue; } Bst_Node *newNode = (Bst_Node*)malloc(sizeof(Bst_Node)); strcpy(newNode->s1, s1); strcpy(newNode->s2, s2); newNode->left = NULL; newNode->right = NULL; P->right = newNode; return 1; } else if(strcmp(s1,P->s1) == 0) { if(strcmp(P->s2, s2) == 0) { return 0; } else if(P->right != NULL) { P = P->right; } else if(P->right == NULL) { Bst_Node *newNode = (Bst_Node*)malloc(sizeof(Bst_Node)); strcpy(newNode->s1, s1); strcpy(newNode->s2, s2); newNode->left = NULL; newNode->right = NULL; P->right = newNode; return 1; } } } } int FindBstTree(char s1[],char s2[]) { int minDp = 99999999; Bst_Node *P = bst_root; while(1) { if(P == NULL && minDp == 99999999) { return -1; } else if(P == NULL && minDp!= 99999999) { return minDp; } else if(strcmp(s1,P->s1) < 0) { P = P->left; continue; } else if(strcmp(s1,P->s1) > 0) { P = P->right; continue; } else if(strcmp(s1,P->s1) == 0) { int dp = editdistDP(P->s2, s2); if(dp < minDp) { minDp = dp; } P = P->right; } } } void printBstTree(char s1[], char s2[], int Dp) { char toPrint[1000][38]; int print_num = 0; Bst_Node *P = bst_root; int needComma = 0; while(1) { if(P == NULL) { break; } else if(strcmp(s1, P->s1) < 0) { P = P->left; continue; } else if(strcmp(s1, P->s1) > 0) { P = P->right; continue; } else if(strcmp(s1,P->s1) == 0) { if(editdistDP(P->s2, s2) == Dp) { strcpy(toPrint[print_num++], P->s2); } P = P->right; } } qsort(toPrint, print_num, sizeof(toPrint[0]), cmp); int i=0; while(i < print_num) { if(needComma) { printf(","); } printf("%s",toPrint[i]); needComma = 1; i++; } } int cmp(const void *a, const void *b) { const char (*p1)[38] = (const char (*)[38])a; const char (*p2)[38] = (const char (*)[38])b; return strcmp(*p1, *p2); }帮我减少这段代码的运行时间
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值