UVa 1328 Period

方法:kmp

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71
 72
 73 void get_fail(const string &str, int *f)
 74 {
 75     f[0] = f[1] = 0;
 76     int n = str.length();
 77     for (int i = 1; i < n; ++i)
 78     {
 79         int j = f[i];
 80         while (j && str[i] != str[j]) j = f[j];
 81         f[i+1] = (str[i]==str[j])?j+1:0;
 82     }
 83 }
 84 int f[1000005];
 85 int n;
 86 string str;
 87 int main()
 88 {
 89     ios::sync_with_stdio(false);
 90     cin.tie(0);
 91     int kase = 0;
 92     while (cin >> n && n)
 93     {
 94         cin >> str;
 95         get_fail(str, f);
 96         cout << "Test case #" << ++kase << newline;
 97         for (int i = 2; i <= n; ++i)
 98         {
 99             int len = i-f[i];
100             if (f[i] != 0 && (i)%len == 0)
101                 cout << i << " " << (i)/len << newline;
102         }
103         cout << newline;
104     }
105 }

时间: 2024-10-12 18:51:23

UVa 1328 Period的相关文章

UVA 1328 - Period KMP

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131 题意:给出一个长度为n的字符串,要求找到一些i,满足说从1~i为多个个的重复子串构成,并输出子串的个数. 题解:对kmp中预处理的数组的理解 //作者:1085422276 #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #i

uva 1328 - Period (周期串的判断 kmp)

题意:给一个长为n的字符串,问字符串的前缀是不是周期串,如果是周期串,输出前缀的最后一个字母的位置和最短周期 思路:kmp字符串匹配的性质运用. 对于前i个字符,如果f[i]不等于零,说明在此字符串的前缀中,有一部分[0,f[i]]和本字符串[i-f[i],i]的这一部分是相同的.如果这i个字符组成一个周期串,那么错开的一部分[f[i],i]恰好是一个循环节.(换句话说,如果满足f[i]不等于零 && [f[i],i]是循环节这两点,就可以说明前i个字符组成一个周期串) code: #in

UVA 1371 - Period(DP)

6.4 一些说明 数据属性可以重写同名的方法属性.这是为了避免在大型系统中产生问题的意外名称冲突.所以用一些减少冲突的常用方法是很有效果的.常用的方法包括:大写字母方法名称,用唯一的字符串来做为数据属性的名称(可以是个下划线_)或者用动词命名方法和用名字命名数据属性. 数据属性就像和对象的普通用户一样可以被方法引用.换句话说,类不能用来实现纯净的数据类型.事实上,在python中不能强制数据隐藏,一切基于约定.(另一方面,如C中写的,python的实现可以做到完全隐藏实现细节并且在必要是可以控制

uva 1371 - Period(二分+dp)

题目链接:uva 1371 - Period 题目大意:给出两个字符串A,B将B分解成若干个子字符串,然后每个子字符串都要变成字符串A,所有子串中编辑最多的次数即为当前状态下的最大编辑次数,要求求最小的最大编辑次数. 解题思路:二分答案,用dp判断,主要是dp判断,dp[i][j]表示到1~i的字符串匹配到j的最大编辑次数,然后考虑分段的时候只要dp[i][0] < mid,那么就可以将dp[i][0] 置0,表示做为起点. #include <cstdio> #include <

UVa 1328 (KMP求字符串周期) Period

当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是一致的. 1 #include <cstdio> 2 3 const int maxn = 1000000 + 10; 4 char s[maxn]; 5 int f[maxn]; //失配函数 6 7 int main() 8 { 9 //freopen("in.txt",

uva 1328(kmp)

被打哭了......神特么kmp,还能这么用!看来我理解的还不够透彻,这里最神奇的就是只有相同的字串才能往下延伸,而"i-f[i]"就正好是前面空出来的模板块,也就是f[i]中不计数的那块,而字符串都是从0开始的,对应的往下加一个却正好成立构成倍数关系 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace

UVa 10087 - The Tajmahal of ++Y2k

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

UVA 1619 Feel Good(DP)

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently developed assigns a non-negat

UVA 11947 Cancer or Scorpio 水题

Cancer or Scorpio Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3098 Description Alice and Michael is a young couple who are planning on having their