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;
评测结果
测试点
|
0 | 答案正确 | 1 | 384 | 10/10 |
1 | 答案正确 | 1 | 412 | 10/10 |
2 | 答案正确 | 1 | 384 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 运行超时 | | | 0/4 |
5 | 答案正确 | 28 | 9812 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 292 | 10/10 |
1 | 答案正确 | 1 | 384 | 10/10 |
2 | 答案正确 | 1 | 300 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 运行超时 | | | 0/4 |
5 | 答案正确 | 33 | 9836 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 420 | 10/10 |
1 | 答案正确 | 1 | 420 | 10/10 |
2 | 答案正确 | 1 | 420 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 内存超限 | | | 0/4 |
5 | 答案正确 | 19 | 9836 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 428 | 10/10 |
1 | 答案正确 | 1 | 304 | 10/10 |
2 | 答案正确 | 1 | 384 | 4/4 |
3 | 异常退出 | 12 | 2436 | 0/3 |
4 | 段错误 | 13 | 2436 | 0/4 |
5 | 答案正确 | 27 | 2436 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 416 | 10/10 |
1 | 答案正确 | 1 | 348 | 10/10 |
2 | 答案正确 | 1 | 412 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 运行超时 | | | 0/4 |
5 | 答案正确 | 110 | 384 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 304 | 10/10 |
1 | 答案正确 | 1 | 384 | 10/10 |
2 | 答案正确 | 1 | 384 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 运行超时 | | | 0/4 |
5 | 答案正确 | 145 | 5636 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 384 | 10/10 |
1 | 答案正确 | 1 | 344 | 10/10 |
2 | 答案正确 | 1 | 256 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 运行超时 | | | 0/4 |
5 | 答案正确 | 41 | 2536 | 4/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;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 308 | 10/10 |
1 | 答案正确 | 1 | 420 | 10/10 |
2 | 答案正确 | 1 | 384 | 4/4 |
3 | 运行超时 | | | 0/3 |
4 | 运行超时 | | | 0/4 |
5 | 答案正确 | 105 | 2432 | 4/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;
}