#include <stdio.h> #include <string.h>

#define MAXN 1005 #define MAXL 25

char word[MAXN][MAXL]; // 存储输入的单词 char prefix[MAXN][MAXL]; // 存储每个单词的最短前缀

int main() { int n = 0; // 输入的单词个数 while (scanf("%s", word[n]) != EOF) { int len = strlen(word[n]); int cnt = 0; // 记录有多少个单词与当前单词的前缀匹配 for (int i = 1; i <= len; i++) { // 枚举单词的前缀 char p[MAXL]; strncpy(p, word[n], i); p[i] = '\0'; // 取出单词的前缀 int flag = 1; // 标记是否有其他单词与当前单词的前缀匹配 for (int j = 0; j < n; j++) { if (j == n) continue; // 排除自身 if (strncmp(word[j], p, i) == 0) { // 当前单词的前缀与其他单词的前缀匹配 flag = 0; break; } } if (flag) { // 没有其他单词与当前单词的前缀匹配,说明找到了最短前缀 strcpy(prefix[n], p); break; } } printf("%s %s\n", word[n], prefix[n]); n++; } return 0;

写出c语言代码:题目描述A prefix of a string is a substring starting at the beginning of the given string The prefixes of carbon are c ca car carb carbo and carbon Note that the empty string is not considered a p

原文地址: https://www.cveoy.top/t/topic/ffqX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录