HRBUST1109(KMP)
来源:互联网 发布:retrofit源码解析 编辑:程序博客网 时间:2024/05/16 10:09
店长终极推荐
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %lld , %llu Java class name: Main
[Submit] [Status] [Discuss]
题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=14083
Description
玩腻了两个工具之后,店长决定要好好学习.玩工具伤身体啊!!店长决定教大家学习阅读,店长总能总学习中找到乐趣,无聊的他找了一篇文章,由各种字符组成,他想统计下这篇阅读中,出现最多次数的相邻两个字符组合是什么?
Input
第一行输入一个整数T表示测试数据组数
接下来输入一行,包括一篇文章,文章由最多不超过200个字符(由任意ascii字符组成)
当T=0时结束
Output
统计文章中出现频度最高的两个连续字符,并且输入然后换行.
如果最高的有多组,按照字典顺序排序后输出第一个.
每T组测试数据后,需要输出一个换行
Sample Input
1
%#@!%#
2
This is a test!
Dianzhang JiaoZhu and Jiashou
Sample Output
%#
is
an解题思路:
相邻的两个字符作为模式串,整个串作为文本串,进行KMP模式匹配,记录匹配次数,更新次数的最大值,同时保存相应的
模式串。
注意每组后给一个空行。
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;string str;char th[2];int solve(string p , string t){ int n = p.size(); vector<int> next(n + 1 , 0); for(int i = 1 ; i < n ; ++ i ) { int j = i; while(j > 0) { j = next[j]; if(p[j] == p[i]) { next[i + 1] = j + 1; break; } } } vector<int> pos; int m = t.size(); for(int i = 0 , j = 0 ; i < m ; ++ i ) { if(j < n && t[i] == p[j]) j ++; else { while(j > 0) { j = next[j]; if(t[i] == p[j]) { j ++; break; } } } if( j == n) pos.push_back(i - n + 1); } return pos.size();}int main(){ #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int n; while(~scanf("%d",&n)) { if(n == 0) break; while(n--) { getchar(); getline(cin , str); int len = str.length(); int maxx = -INF; string res; for(int i = 1 ; i < len ; i ++) { string s = ""; th[0] = str[i-1]; th[1] = str[i]; s = (string)th; int cnt = solve(s , str); if(cnt > maxx) { res = s; maxx = cnt; } else if(cnt == maxx) { if(s < res) { res = s; } } } cout << res <<endl; } printf("\n"); }}
0 0
- HRBUST1109(KMP)
- KMP
- KMP
- KMP
- KMP
- KMP
- KMP
- kmp
- kmp
- KMP
- kmp
- KMP
- KMP
- KMP
- kmp
- KMP
- KMP
- kmp
- Java解决编程猫狗鱼过河问题
- 个回复的回复的华国锋的郭德纲
- 安装DBDesigner4之路 for linux mint(ubuntu)
- 和规范化个凤凰股份合格的风格的师傅都是
- Customizing Navigation Bar and Status Bar in iOS 7 .
- HRBUST1109(KMP)
- 优化Android App性能?十大技巧
- 菜鸟运维笔记:安装MySQL,PHP及phpMyAdmin
- HDOJ-2044-一只小蜜蜂【fib数列】
- 牛人和一般人的思维(图)
- 无形的力量,有形的手
- LeetCode 36 Sudoku Solver
- Constant folding 常量折叠
- PHP+SMTP的邮件发送机制