dongqiu3709 2012-10-14 09:56
浏览 5
已采纳

需要算法和实现帮助 - 比较2个文件[关闭]

My Project Purpose : There are 4 files. Each of them has different number of lines. Each line consists of a single or few words. Now, for each of these files, I want to count which other file has the maximum common words.

  • eg. (',' is a new line )
  • Input:
  • File 1 Lines : A,B,C,D
  • File 2 Lines : C,D,E,F
  • File 3 Lines : A,E,C,G
  • File 4 Lines : C,E,F,A

  • Output :

  • File 1 : Maximum common words is 2 and they are in the files : File 2 (C,D), File 3 (A,C) and File 4 (C,A).
  • File 2 : Maximum common words is 3 and they are in the files : File 4 (C,E,F).
  • File 3 : Maximum common words is 3 and they are in the files : File 4 (C,E,A).
  • File 4 : Maximum common words is 3 and they are in the files : File 2 (C,E,F).

My logic :

  1. Start
  2. Read each line from file and store it in memory as a 1-D array (eg. array1[0] = "A", array1[1] = "B" and so on.
  3. Since there are 4 files, I create 4 arrays = array1 to array4. Each of them will have the contents of their corresponding files.
  4. Now I will compare the first words in the first array with the first word in the second array.
  5. Now I will compare the first words in the first array with the second word in the second array and so on till the end on second array.
  6. I will continue this till the last word in the last array.
  7. When ever I found something was matching I will note down in a variable by incrementing 1.

Wish to know if this is the right of approaching this problem.

Or is there a better way to think at this problem?

Edits : 1. Forgot to add, Will be using php.

  • 写回答

3条回答 默认 最新

  • doujiu9307 2012-10-14 10:20
    关注

    Should be easily done with array_intersect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系