1005. Programming Pattern (35)【待解决】

1005. Programming Pattern (35)

时间限制
600 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
HOU, Qiming

Programmers often have a preference among program constructs. For example, some may prefer "if(0==a)", while others may prefer "if(!a)". Analyzing such patterns can help to narrow down a programmer's identity, which is useful for detecting plagiarism.

Now given some text sampled from someone's program, can you find the person's most commonly used pattern of a specific length?

Input Specification:

Each input file contains one test case. For each case, there is one line consisting of the pattern length N (1<=N<=1048576), followed by one line no less than N and no more than 1048576 characters in length, terminated by a carriage return '\n'. The entire input is case sensitive.

Output Specification:

For each test case, print in one line the length-N substring that occurs most frequently in the input, followed by a space and the number of times it has occurred in the input. If there are multiple such substrings, print the lexicographically smallest one.

Whitespace characters in the input should be printed as they are. Also note that there may be multiple occurrences of the same substring overlapping each other.

Sample Input 1:
4
//A can can can a can.
Sample Output 1:
 can 4
Sample Input 2:
3
int a=~~~~~~~~~~~~~~~~~~~~~0;
Sample Output 2:
~~~ 19
2015-10-11 把最后一种的HanShuCi注释掉,还花费35ms,2476kb;再把getline注释掉花费1ms,482kb。解决方案需要考虑一番

http://blog.csdn.net/jtjy568805874/article/details/50759503#comments

上面的连接里面有个ac的。搜到的关于 Inv()的相关知识是找逆元。然后有个更通俗的解释http://blog.csdn.net/cqlf__/article/details/7953039
(a/b)%Mod=c;    (b*p)%Mod=1;    ==》   (a/b)*(b*p) %Mod=c;    ==》    (a*p)%Mod=c
应用,求(a*p)%MOD,但是(a*p)会超出数据范围。然后找到一个b使得(b*p)%MOD=1;

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月20日 22:48部分正确281005C++ (g++ 4.7.2)289812datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确138410/10
1答案正确141210/10
2答案正确13844/4
3运行超时  0/3
4运行超时  0/4
5答案正确2898124/4
#include<iostream>  
#include<string>
#include<vector> 
#include<queue>
#define MALEN 300
#define NOExist -1
using namespace std;
int cmpR(string *str, int a, int b,int pos)
{
  int times = 0; 
  while (times<pos)
  {
    if ((*str)[a + times] > (*str)[b + times])
      return 1;
    else if ((*str)[a + times] < (*str)[b + times])
      return -1; 
    times++;
  }
  return 0;
}
int main()
{
  int N,count=0,index,len;
  int ans;
  cin >> N;
  string str;
  char cc;
  vector<int>ReSame(MALEN, NOExist);
  vector<int>beforeSame;
  getchar();
  for (index = 0; (cc = getchar()) != '\n';index++)
  {
    beforeSame.push_back(NOExist);
    if (cc >= MALEN)return 0;
    str += cc;  
    beforeSame[index]=ReSame[cc];
    ReSame[cc] = index; 
  }  
  len = str.size();
  for (index = 0; index < MALEN; index++)
  {
    queue<int>q;
    int temp = ReSame[index];
    while (temp!= NOExist)
    {
      if (temp <=str.size() - N)
        q.push(temp);
      temp = beforeSame[temp];
    }
    while (!q.empty())
    {
      temp = q.front();
      q.pop();
      int  size = q.size();
      int Cnt = 1;
      len--;
      while (size+1>=count&&size--)
      { 
        if (cmpR(&str, temp, q.front(), N) == 0)
        {
          Cnt++;
          len--;
        }
        else q.push(q.front());
        q.pop();  
      }
      if (Cnt > count || Cnt == count&&cmpR(&str,ans, temp , N)>0)
      {
        count = Cnt;
        ans = temp;
      } 
    }
      if (count > len)break;
  }
  for (index = 0; index < N; index++)
    printf("%c", str[ans + index]);
  printf(" %d", count);
  system("pause");
  return 0;
} 

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月20日 22:45部分正确281005C++ (g++ 4.7.2)339836datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确129210/10
1答案正确138410/10
2答案正确13004/4
3运行超时  0/3
4运行超时  0/4
5答案正确3398364/4
#include<iostream>  
#include<string>
#include<vector> 
#include<map>
#include<queue> 
#define NOExist -1
using namespace std;
int cmpR(string *str, int a, int b, int pos)
{
  int times = 0;
  while (times<pos)
  {
    if ((*str)[a + times] >(*str)[b + times])
      return 1;
    else if ((*str)[a + times] < (*str)[b + times])
      return -1;
    times++;
  }
  return 0;
}
int main()
{
  int N, count = 0, index, len;
  int ans;
  cin >> N;
  string str;
  char cc;
  map<char,int>ReSame;
  vector<int>beforeSame;
  getchar();
  for (index = 0; (cc = getchar()) != '\n'; index++)
  {
    beforeSame.push_back(NOExist); 
    str += cc;
    map<char, int>::iterator RQ = ReSame.find(cc);
    if (RQ!=ReSame.end())
    beforeSame[index] = ReSame[cc];
    else beforeSame[index] = NOExist;
    ReSame[cc] = index;
  }
  len = str.size();
  for (map<char, int>::iterator index = ReSame.begin(); index != ReSame.end(); index++)
  {
    queue<int>q;
    int temp =index->second;
    while (temp != NOExist)
    {
      if (temp <= str.size() - N)
        q.push(temp);
      temp = beforeSame[temp];
    }
    while (!q.empty())
    {
      temp = q.front();
      q.pop();
      int  size = q.size();
      int Cnt = 1;
      len--;
      while (size + 1 >= count&&size--)
      {
        if (cmpR(&str, temp, q.front(), N) == 0)
        {
          Cnt++;
          len--;
        }
        else q.push(q.front());
        q.pop();
      }
      if (Cnt > count || Cnt == count&&cmpR(&str, ans, temp, N)>0)
      {
        count = Cnt;
        ans = temp;
      }
    }
    if (count > len)break;
  }
  for (index = 0; index < N; index++)
    printf("%c", str[ans + index]);
  printf(" %d", count);
  system("pause");
  return 0;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月20日 22:31部分正确281005C++ (g++ 4.7.2)199836datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确142010/10
1答案正确142010/10
2答案正确14204/4
3运行超时  0/3
4内存超限  0/4
5答案正确1998364/4
#include<iostream>  
#include<string>
#include<vector> 
#include<queue>
#define MALEN 300
#define NOExist -1
using namespace std;
int cmpR(string *str, int a, int b,int pos)
{
  int times = 0; 
  while (times<pos)
  {
    if ((*str)[a + times] > (*str)[b + times])
      return 1;
    else if ((*str)[a + times] < (*str)[b + times])
      return -1; 
    times++;
  }
  return 0;
}
void DFS_str(string *str, int now, int*count, queue<int>*q, int *ans,int N)
{ 
  vector<int>ReSame(MALEN, NOExist);
  vector<int>beforeSame((*str).size(), NOExist);
  int index;
  int len = (*q).size();
  while (!(*q).empty())
  {   
    index=(*q).front() + now;    
    beforeSame[index] = ReSame[(*str)[index]];
    ReSame[(*str)[index]] = index;
    (*q).pop();
  } 
  for (index = 0; index < MALEN; index++)
  {
    int temp = ReSame[index];
    while (temp != NOExist)
    {
      if (temp <= (*str).size() - N + now)
      {
        len--;
        (*q).push(temp - now);
      }
      temp = beforeSame[temp];
    }
    if (!(*q).empty()&&(*q).size()>=(*count))
    {
     if (now < N-1 && (*q).size()>1 )
      DFS_str(str, now + 1, count, q, ans, N);
     else if (  ((*q).size() > (*count) || (*q).size() ==(*count)&&cmpR(str, (*ans), (*q).front(), N) > 0))
    {
      (*count) = (*q).size();
      (*ans) = (*q).front();
    } 
    }
    if (len < (*count))return;
    while (!(*q).empty()) 
      (*q).pop();
  }
}
int main()
{
  int N,count=0,index,len;
  int ans;
  cin >> N;
  string str;
  char cc;
  vector<int>ReSame(MALEN, NOExist);
  vector<int>beforeSame;
  getchar();
  for (index = 0; (cc = getchar()) != '\n';index++)
  {
    beforeSame.push_back(NOExist);
    if (cc >= MALEN)return 0;
    str += cc;  
    beforeSame[index]=ReSame[cc];
    ReSame[cc] = index; 
  }  
  len = str.size()-N+1;
  for (index = 0; index < MALEN; index++)
  {
    queue<int>q;
    int temp = ReSame[index];
    while (temp!= NOExist)
    { 
      if (temp <= str.size() - N)
      {
        len--;
        q.push(temp);
      }
      temp = beforeSame[temp];
    }
    if (!q.empty()&&q.size()>=count)
    { 
      if (0<N - 1 && q.size()>1)
      DFS_str(&str, 1, &count, &q, &ans, N);
      else if ((q.size() > count || q.size() == count&&cmpR(&str, ans, q.front(), N)>0))
     {
      count = q.size();
      ans = q.front();
     } 
    }
    if (len < count)break;
  }
  for (index = 0; index < N; index++)
    printf("%c", str[ans + index]);
  printf(" %d", count);
  system("pause");
  return 0;
}


评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月17日 00:32部分正确281005C++ (g++ 4.7.2)272436datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确142810/10
1答案正确130410/10
2答案正确13844/4
3异常退出1224360/3
4段错误1324360/4
5答案正确2724364/4
#include<iostream>  
#include<string>
#include<vector>
#define MALEN 256
using namespace std;
int main()
{
  int N;
  cin >> N;
  string str;
  char cc;
  vector<int>cRq(MALEN, 0);
  getchar();
  for (; (cc = getchar()) != '\n';)
  {
    str += cc;
    cRq[cc] = 1;
  }
  int temp = 0;
  vector<char>int_char;
  int index;
  bool fistd = true;
  for (index = 0; index < MALEN; index++)
  {
    if (fistd && 0 != cRq[index])
    {
      fistd = false;
      temp++;
      int_char.push_back(index);
    }
    else if (!fistd && 0 == cRq[index])
      cRq[index] = cRq[index - 1];
    else if (!fistd)
    {
      cRq[index] = cRq[index - 1] + 1;
      temp++;
      int_char.push_back(index);
    }
  }
  temp++;
  int t = N;
  long long radix = 1;
  for (index = 1; index < N; index++)
  {
    radix *= temp;
  }
  vector<int>maxArray(radix*temp, 0);
  N--;
  int goal, count = 0;

  for (index = 0, t = 0; index <str.size(); index++)
  {
    if (index < N)
      t = t*temp + cRq[str[index]];
    else
    {
      t = t*temp + cRq[str[index]];
      maxArray[t]++;
      if (maxArray[t] > count || maxArray[t] == count && t < goal)
      {
        goal = t;
        count = maxArray[t];
      }
      t %= radix;
    }
  }
  t = goal;
  N++;
  while (N--)
  {
    t = goal / radix;
    printf("%c", int_char[t - 1]);
    goal %= radix;
    radix /= temp;

  }
  printf(" %d", count);
  system("pause");
  return 0;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月12日 18:18部分正确281005C++ (g++ 4.7.2)110416datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确141610/10
1答案正确134810/10
2答案正确14124/4
3运行超时  0/3
4运行超时  0/4
5答案正确1103844/4
#include<iostream> 
#include<map> 
#include<vector> 
#include<string>
using namespace std;
int main()
{
  int Ge,t;
  string goalstring;
  int maxlenth = 0;
  scanf("%d", &Ge); 
  getchar();
  map<string, int>strT;
  string str; 
   t=Ge ;
  for (char c; (c = getchar()) != '\n';)
  {
    if (Ge > 1)
    {
      str += c;
      Ge--;
    }
    else
    {
      str += c;
      map<string, int>::iterator RQ = strT.find(str);
      int temp;
      if (RQ != strT.end())
        temp = RQ->second = RQ->second + 1;
      else
      {
        temp = 1;
        strT[str] = 1;
      }
      if (temp > maxlenth || temp == maxlenth&&goalstring > str)
      {
        maxlenth = temp;
        goalstring = str;
      }
      str.erase(0, 1);
    }
  }
  for (int i = 0;i<t; i++)
  { 
    putchar(goalstring[i]);
    } 
    printf(" %d\n",maxlenth);
  system("pause");
  return 0;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月11日 00:49部分正确281005C++ (g++ 4.7.2)1455636datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130410/10
1答案正确138410/10
2答案正确13844/4
3运行超时  0/3
4运行超时  0/4
5答案正确14556364/4
#include<iostream> 
#include<string>   
#include<vector>
#include<algorithm>
using namespace std;
class CmpDanCi
{
private:
  int num;
  string *DiZhi;
public:
  CmpDanCi(int Ge, string *ss) :num(Ge), DiZhi(ss){}
  bool operator()(const int &A, const int &B)
  {
    int i = 0;
    while (i < num && (*DiZhi)[i + A] == (*DiZhi)[i + B])i++; 
      return (*DiZhi)[i + A] < (*DiZhi)[i + B];
  }
};
bool Equalstr(string *a,   int from,int k,int Ge)
{ 
  for (int index = 0; index < Ge; index++)
    if ((*a)[k+index] != (*a)[from + index])return false;
  return true;
}
void HanShuCi(vector<int>* DanCi, int end)
{
  int Cong = 0; 
  while (Cong < end)
  { 
    (*DanCi)[Cong] = Cong;
    Cong++;
  }
}
int main()
{
  string YiChan;
  int Ge;
  cin >> Ge;
  getchar();/*吸收换行*/
  getline(cin, YiChan);

  int end = YiChan.size() - Ge + 1;
  end = end > 0 ? end : 0;
  vector<int>DanCi(end);
  HanShuCi(&DanCi, end);

  sort(DanCi.begin(), DanCi.end(), CmpDanCi(Ge, &YiChan));

  int goalstring;
  int maxlenth = 0;

  for (int index = 0; index < end;)
  {
    int count = 1;
    int td;
    for (td = index + 1; td < end&&Equalstr(&YiChan,   DanCi[index], DanCi[td], Ge) ; td++)
      count++;
    if (maxlenth < count || maxlenth == count&&Equalstr( & YiChan , DanCi[index],  goalstring,   Ge) )
    {
      maxlenth = count;
      goalstring = DanCi[index];
    }
    index = td;
  }
  cout << YiChan.substr(goalstring, Ge) << " " << maxlenth << endl;
  system("pause");
  return 0;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月10日 22:51部分正确281005C++ (g++ 4.7.2)412536datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确138410/10
1答案正确134410/10
2答案正确12564/4
3运行超时  0/3
4运行超时  0/4
5答案正确4125364/4
#include<iostream> 
#include<string>   
#include<vector>
using namespace std;
bool Equalstr(string *a, string*b, int from)
{ 
  for (int index = 0; index < (*b).size(); index++)
    if ((*b)[index] != (*a)[from + index])return false;
  return true;
}
void HanShuCi(string* YiChan, int Ge, int& maxlenth, string &goalstring)
{
  int Cong = 0;
  int temp;
  int end=(* YiChan).size()-Ge+1; 
  string BuFen;
  vector<bool>NoUsed(end, true);
  while (Cong < end)
  { 
    if (NoUsed[Cong])
    {
      BuFen = (*YiChan).substr(Cong, Ge);
      temp = 1;
      for (int Dang = Cong + 1; Dang<end; Dang++)
      { 
        if (NoUsed[Dang] && Equalstr(YiChan, &BuFen, Dang))
        {
          NoUsed[Dang] = false;
          temp++;
        }
      }
    if (temp > maxlenth || temp == maxlenth&&goalstring> BuFen)
    {
      maxlenth = temp;
      goalstring = BuFen;
    }
    }
    Cong++;
  }
}
int main()
{
  string YiChan;
  int Ge;
  cin >> Ge;
  getchar();/*吸收换行*/
  getline(cin, YiChan);
  int maxlenth = 0;
  string goalstring;
  HanShuCi(&YiChan, Ge, maxlenth, goalstring);
  cout << goalstring << " " << maxlenth << endl;
  system("pause");
  return 0;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
10月09日 21:20部分正确281005C++ (g++ 4.7.2)1052432datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130810/10
1答案正确142010/10
2答案正确13844/4
3运行超时  0/3
4运行超时  0/4
5答案正确10524324/4
#include<iostream> 
#include<string>   
#include<vector>
using namespace std; 
void HanShuCi(string YiChan, int Ge,int& maxlenth,string &goalstring)
{ 
  int Cong = 0;
  int temp;
  int ChangDu = YiChan.size();
  string BuFen;
  vector<pair<string,int>>RongQi;
  while (Cong + Ge<= ChangDu)
  {
    BuFen = YiChan.substr(Cong, Ge);
    Cong++; 
    vector<pair<string, int>>::iterator YouBiao = RongQi.begin();
    for (; YouBiao != RongQi.end() && YouBiao->first != BuFen; YouBiao++);

    if (YouBiao == RongQi.end())
    {
      RongQi.push_back(make_pair(BuFen, 1));
        temp = 1;
    }
    else   
      temp =YouBiao->second=YouBiao->second+1;  
    if (temp > maxlenth || temp == maxlenth&&goalstring > BuFen)
    {
      maxlenth = temp;
      goalstring = BuFen;
    }
  }
}
int main()
{ 
  string YiChan;
  int Ge;
  cin >> Ge;
  getchar();/*吸收换行*/
  getline(cin, YiChan); 
  int maxlenth=0;
  string goalstring;
  HanShuCi(YiChan, Ge, maxlenth, goalstring); 
  cout << goalstring << " " << maxlenth << endl;
  system("pause");
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值