[C++] common_sub_str →→→→→进入此内容的聊天室

来自 , 2020-02-24, 写在 C++, 查看 164 次.
URL http://www.code666.cn/view/cb79f8fa
  1. #include<iostream>
  2. #include<cstring>
  3. #include<strings.h>
  4. #include<vector>
  5. using namespace std;
  6.  
  7. //int map[3000][3000];
  8.  
  9. const string lcs(const string& str1,const string& str2);
  10. const vector<string> lcs_vt(const string &str1, const string &str2);
  11. const vector<string> n_lcs(vector<string> &strs);
  12. vector<string> split_with_string(const string& str, const string& delim);
  13. int print_vt(vector<string> &vt);
  14. int vector_unique(vector<string> &vt);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.         //string lcs=lcs(argv[1],argv[2]);
  19.         char buf[102400];
  20.         vector<string> substr_vt;
  21.         vector<string> strs;
  22.  
  23.         while(fgets(buf, 102400, stdin)) {
  24.                 if (buf[strlen(buf) - 1] == '\n') {
  25.                         buf[strlen(buf) - 1] = 0;
  26.                 }
  27.                 string temp = buf;
  28.                 strs.push_back(temp);
  29.         }
  30.  
  31.         substr_vt = n_lcs(strs);
  32.         vector_unique(substr_vt);
  33.         cout << "-------------------------------" << endl;
  34.         for(unsigned int i = 0; i < substr_vt.size(); i++) {
  35.                 cout << substr_vt[i] << endl;
  36.         }
  37.         cout << "-------------------------------" << endl;
  38.         return 0;
  39. }
  40.  
  41. const vector<string> n_lcs(vector<string> &strs)
  42. {
  43.         vector<string> substr_vt;
  44.         vector<string> temp_substr_vt;
  45.         substr_vt.push_back(strs[0]);
  46.  
  47.         for(unsigned int i = 1; i < strs.size(); i++) {
  48.                 fprintf(stderr, "strs.size:%d, substr_vt.size:%d\n", strs.size(), substr_vt.size());
  49.                 for(unsigned int j = 0; j < substr_vt.size(); j++) {
  50.                         vector<string> temp_vt;
  51.                         //fprintf(stderr, "befre lcs_vt\n");
  52.                         temp_vt = lcs_vt(substr_vt[j], strs[i]);
  53.                         //fprintf(stderr, "after lcs_vt\n");
  54.  
  55.                         vector_unique(temp_vt);
  56.                         for(unsigned int k = 0; k < temp_vt.size(); k++) {
  57.                                 temp_substr_vt.push_back(temp_vt[k]);
  58.                         }
  59.                 }
  60.                 vector_unique(temp_substr_vt);
  61.                 //print_vt(temp_substr_vt);
  62.                 substr_vt.clear();
  63.                 substr_vt = temp_substr_vt;
  64.                 temp_substr_vt.clear();
  65.         }
  66.  
  67.         /*
  68.         int max_length = 0;
  69.         vector<int> max_index_vt;
  70.         for(unsigned int i = 0; i < substr_vt.size(); i++) {
  71.                 if (substr_vt[i].length() > max_length) {
  72.                         max_length = substr_vt[i].length();
  73.                 }
  74.         }
  75.         temp_substr_vt.clear();
  76.         for(unsigned int i = 0; i < substr_vt.size(); i++) {
  77.                 if (substr_vt[i].length() == max_length) {
  78.                         //max_index_vt.push_back(i);
  79.                         temp_substr_vt.push_back(substr_vt[i]);
  80.                 }
  81.         }
  82.  
  83.         return temp_substr_vt;
  84.         */
  85.         return substr_vt;
  86. }
  87.  
  88. const vector<string> lcs_vt(const string &str1, const string &str2)
  89. {
  90.         int col_num = str1.length();
  91.         int row_num = str2.length();
  92.         int map[row_num][col_num];
  93.  
  94.         bzero(map, sizeof(map));
  95.         vector<string> ret;
  96.  
  97.         for(int i = 0; i < col_num; i++) {
  98.                 for(int j = 0; j < row_num; j++) {
  99.                         if (str1[i] == str2[j]) {
  100.                                 map[j][i] = 1;
  101.                         }
  102.                 }
  103.         }
  104.  
  105.         int list_flag = 0;
  106.         string temp;
  107.         for (int i = col_num - 1; i >= 0; i--) {
  108.                 int j = 0;
  109.                 temp = "";
  110.                 while(j + i < col_num && j < row_num) {
  111.                         if (map[j][j + i] == 1) {
  112.                                 temp += str1[j + i];
  113.                         } else {
  114.                                 temp += '#';
  115.                         }
  116.                         j++;
  117.                 }
  118.                 //if (!(temp.empty())) {
  119.                 //      cout << "###" << temp << endl;
  120.                 //}
  121.                 vector<string> splited_str = split_with_string(temp, "#");
  122.                 vector_unique(splited_str);
  123.                 for(unsigned int i = 0; i < splited_str.size(); i++) {
  124.                         ret.push_back(splited_str[i]);
  125.                 }
  126.         }
  127.  
  128.         for (int i = 1; i < row_num; i++) {
  129.                 int j = 0;
  130.                 temp = "";
  131.                 while(j < col_num && j + i < row_num) {
  132.                         if (map[j + i][j] == 1) {
  133.                                 temp += str2[j + i];
  134.                         } else {
  135.                                 temp += '#';
  136.                         }
  137.                         j++;
  138.                 }
  139.                 //if (!(temp.empty())) {
  140.                 //      cout << "###" << temp << endl;
  141.                 //}
  142.                 vector<string> splited_str = split_with_string(temp, "#");
  143.                 vector_unique(splited_str);
  144.                 for(unsigned int i = 0; i < splited_str.size(); i++) {
  145.                         ret.push_back(splited_str[i]);
  146.                 }
  147.         }
  148.        
  149.         /*
  150.         cout << "======================================" << endl;
  151.         cout << " " << str1 << endl;
  152.         for(int i = 0; i < row_num; i++) {
  153.                 cout << str2[i];
  154.                 for(int j = 0; j < col_num; j++) {
  155.                         cout << map[i][j];
  156.                 }
  157.                 cout << endl;
  158.         }
  159.         cout << "======================================" << endl;
  160.         */
  161.         //cout << "======================================" << endl;
  162.         vector_unique(ret);
  163.         //print_vt(ret);
  164.         //cout << "======================================" << endl;
  165.         return ret;
  166. }
  167.  
  168. const string lcs(const string& str1,const string& str2)
  169. {
  170.         int xlen=str1.size();       //横向长度
  171.         vector<int> tmp(xlen);        //保存矩阵的上一行
  172.         vector<int> arr(tmp);     //当前行
  173.         int ylen=str2.size();       //纵向长度
  174.         int maxele=0;               //矩阵元素中的最大值
  175.         int pos=0;                  //矩阵元素最大值出现在第几列
  176.         for(int i=0;i<ylen;i++){
  177.                 string s=str2.substr(i,1);
  178.                 arr.assign(xlen,0);     //数组清0
  179.                 for(int j=0;j<xlen;j++){
  180.                         if(str1.compare(j,1,s)==0){
  181.                                 if(j==0)
  182.                                         arr[j]=1;
  183.                                 else
  184.                                         arr[j]=tmp[j-1]+1;
  185.                                 if(arr[j]>maxele){
  186.                                         maxele=arr[j];
  187.                                         pos=j;
  188.                                 }
  189.                         }      
  190.                 }
  191.                 tmp.assign(arr.begin(),arr.end());
  192.         }
  193.         string res=str1.substr(pos-maxele+1,maxele);
  194.         return res;
  195. }
  196.  
  197. vector<string> split_with_string(const string& str, const string& delim)
  198. {
  199.         vector<string> substr_vt;
  200.         int where_delim;
  201.         int old_offset = 0;
  202.         string temp;
  203.  
  204.         while((where_delim = str.find(delim, old_offset)) != string::npos) {
  205.                 temp.assign(str, old_offset, where_delim - old_offset);
  206.                 if (!(temp.empty())) {
  207.                         substr_vt.push_back(temp);
  208.                 }
  209.                 old_offset = where_delim + delim.length();
  210.                 where_delim = old_offset;
  211.         }
  212.         temp.assign(str, old_offset, str.length() - old_offset);
  213.         if (!(temp.empty())) {
  214.                 substr_vt.push_back(temp);
  215.         }
  216.  
  217.         return substr_vt;
  218. }
  219.  
  220. int print_vt(vector<string> &vt)
  221. {
  222.         cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  223.         for(unsigned int i = 0; i < vt.size(); i++) {
  224.                 cout << vt[i] << endl;
  225.         }
  226.         cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  227. }
  228.  
  229. int vector_unique(vector<string> &vt)
  230. {
  231.         sort(vt.begin(), vt.end());
  232.         vt.erase(unique(vt.begin(), vt.end()), vt.end());
  233. }
  234.  

回复 "common_sub_str"

这儿你可以回复上面这条便签

captcha