1005. Programming Pattern (35)【待解决】
来源:互联网 发布:软件用户手册编写目的 编辑:程序博客网 时间:2024/05/29 07:26
1005. Programming Pattern (35)
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 4Sample Input 2:
3int 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/101答案正确141210/102答案正确13844/43运行超时 0/34运行超时 0/45答案正确2898124/4
#include<iostream> #include<string>#include<vector> #include<queue>#define MALEN 300#define NOExist -1using 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/101答案正确138410/102答案正确13004/43运行超时 0/34运行超时 0/45答案正确3398364/4#include<iostream> #include<string>#include<vector> #include<map>#include<queue> #define NOExist -1using 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/101答案正确142010/102答案正确14204/43运行超时 0/34内存超限 0/45答案正确1998364/4#include<iostream> #include<string>#include<vector> #include<queue>#define MALEN 300#define NOExist -1using 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/101答案正确130410/102答案正确13844/43异常退出1224360/34段错误1324360/45答案正确2724364/4
#include<iostream> #include<string>#include<vector>#define MALEN 256using 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/101答案正确134810/102答案正确14124/43运行超时 0/34运行超时 0/45答案正确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/101答案正确138410/102答案正确13844/43运行超时 0/34运行超时 0/45答案正确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/101答案正确134410/102答案正确12564/43运行超时 0/34运行超时 0/45答案正确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/101答案正确142010/102答案正确13844/43运行超时 0/34运行超时 0/45答案正确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;}
- 1005. Programming Pattern (35)【待解决】
- 1005. Programming Pattern (35)
- 1005. Programming Pattern (35)解题报告
- pat-top 1005. Programming Pattern (35)
- 【待翻译】Video4Linux Programming
- 待解决
- 待解决
- 待解决
- 待解决
- 待解决
- 待解决
- 待解决
- 待解决
- 待解决
- PAT (Top Level) Practise 1005Programming Pattern (35)
- 5-26 Population (35分) -- 待解决
- Programming Assignment 3: Pattern Recognition
- 待解决的存疑
- Android音频系统之AudioFlinger(二)
- MPI Collective Communications
- 内聚、耦合
- spymemcached测试例子
- 二叉树
- 1005. Programming Pattern (35)【待解决】
- hdu1043 Eight(A*/双向BFS/单项BFS打表+康托展开)
- 黑马程序员—多线程安全问题(锁)
- 最长单词2
- poj1028--动态规划--Ignatius and the Princess III
- Android音频系统之AudioFlinger(三)
- poj1200 Crazy Search
- 组织之瓶颈
- 在Scala中的循环如何“break”