• UVa 10132


      题目大意:有n个相同的文件,每个文件从中间分为两半,现在给你这2n个文件碎片,求原来完整的文件。

      找出文件碎片长度的最大值和最小值,二者相加可得到原来文件的长度len。然后逐个进行拼接,将拼接后长度等于len的加入到map中,最后map中出现次数最多的就是原文件。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <string>
     4 #include <map>
     5 using namespace std;
     6 #define FILEN 150
     7 #define SIZEN 256*8+10
     8 
     9 string str[FILEN];
    10 map<string, int> m;
    11 
    12 int main()
    13 {
    14 #ifdef LOCAL
    15     freopen("in", "r", stdin);
    16 #endif
    17     char s[300];
    18     int T;
    19     scanf("%d", &T);
    20     getchar();
    21     gets(s);
    22     while (T--)
    23     {
    24         int n = 0; 
    25         while (gets(s))
    26         {
    27             if (!s[0])   break;
    28             str[n++] = s;
    29         }
    30         int min_len = 10000, max_len = 0;
    31         for (int i = 0; i < n; i++)
    32         {
    33             if (str[i].size() < min_len)  min_len = str[i].size();
    34             if (str[i].size() > max_len)  max_len = str[i].size();
    35         }
    36         int len = max_len + min_len;
    37         map<string, int>::iterator it;
    38         m.clear();
    39         for (int i = 0; i < n; i++)
    40             for (int j = i+1; j < n; j++)
    41             {
    42                 string t = str[i] + str[j];
    43                 if (t.size() == len)
    44                 {
    45                     it = m.find(t);
    46                     if (it != m.end())  m[t]++;
    47                     else  m[t] = 1;
    48                 }
    49                 t = str[j] + str[i];
    50                 if (t.size() == len)
    51                 {
    52                     it = m.find(t);
    53                     if (it != m.end())  m[t]++;
    54                     else  m[t] = 1;
    55                 }
    56             }
    57         int lmax = 0; 
    58         string ans;
    59         for (it = m.begin(); it != m.end(); it++)
    60             if (it->second > lmax)
    61             {
    62                 lmax = it->second;
    63                 ans = it->first;
    64             }
    65         cout << ans << endl;
    66         if (T)  printf("
    ");
    67     }
    68     return 0;
    69 }
    70 
    71         
    View Code
  • 相关阅读:
    笔记:多线程访问ConcurrentHashMap对key加锁
    根据第三列去重
    Correct the classpath of your application so that it contains a single, compatible version of org.apache.log4j.ConsoleAppender
    python 中将源配置为阿里
    criteria两个 判断
    git flow
    sqlmap用法详解
    MongoDB 入门
    MongoDB 手册
    OWASP TOP 10简单介绍
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3280021.html
Copyright © 2020-2023  润新知