poj 2406 Power Strings

点击打开链接poj2406

思路:kmp+最小循环节

分析:
1 题目要求的是给定一个字符串找到最小循环节的个数,但是这里有个限制的地方就是如果这个字符串不是刚好由n个最小循环节组成那么就认为一整串才是一个循环节
2 题目说了输入的字符是可打印的,所以应该用gets读入,判断终止的时候用strcmp。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

#define MAXN 1000010

char pattern[MAXN];
int next[MAXN];

void getNext(){
   int m = strlen(pattern);
   next[0] = next[1] = 0;
   for(int i = 1 ; i < m ; i++){
      int j = next[i];
      while(j && pattern[i] != pattern[j])
         j = next[j];
      next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;
   }
   int cir = m-next[m];
   if(m%cir == 0)/*刚好由n个最小循环节组成*/
     printf("%d\n" , m/cir);
   else/*否则都是输出1*/
     printf("1\n");
}

int main(){
   while(gets(pattern)){
      if(!strcmp(pattern , "."))
        break;
      getNext();
   }
   return 0;
}
时间: 2024-08-01 18:32:23

poj 2406 Power Strings的相关文章

poj 2406 Power Strings【KMP】

点击打开题目 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33548   Accepted: 13935 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b =

UVa 113 / POJ 2109 Power of Cryptography

使用double处理大整数&泰勒公式与误差分析 113 - Power of Cryptography Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49 http://poj.org/problem?id=2109 Background Curre

算法:poj 4045 Power Station (树形dp)

题意 n个城市节点构成的一棵树,节点i到节点j的电量损耗为 I*I*R*(i到j的路径所含边数),现 在要在某个结点上修建一个供电站,使得这个结点到所有其它节点的总损耗量最小. 思路 典型的树形dp 可以先用一次dfs求出每一点的子树结点个数num[u],以及每一点到它子树所有结点的总 距离f[u][0]; 然后再用一次dfs,推出每个结点到除去它子树部分的结点总距离f[u][1]. f[v][1] = (f[u][0]-f[v][0]-num[v]) + f[u][1] + n - num[v

poj 1459 Power Network

click here ~~ ***Power Network*** Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(

poj 2109 Power of Cryptography

今天懒了,这道题直接去看了大家的帖子,三行搞定... 题目大意: K ^ N = P, 给N 和 P, 求K.数据规模 :1<=n<= 200, 1<=p<10101 and there exists an integer k, 1<=k<=109 . 看到1<=p<10101 我就去想大数操作了,后来发现原来double完全可以放. 类型          长度 (bit)           有效数字          绝对值范围float       

KMP专题【完结】

第一题 hdu 1711 Number Sequence 点击打开hdu 1711 思路: 1 kmp是用来匹配字符串,只能够匹配单一的字符串 2 kmp的算法的过程:   1:假设文本串的长度为n,模式串的长度为m:   2:先例用O(m)的时间去预处理next数组,next数组的意思指的是当前的字符串匹配失败后要转到的下一个状态:   3:利用o(n)的时间去完成匹配: 3 时间复杂度为o(n+m)即o(n): 点击查看代码 第二题 hdu 1686 oulipo 点击打开hdu 1686

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj 题型分类

主流算法: 1.搜索 //回溯 2.DP(动态规划) 3.贪心 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同等安置矩形的并的面积与周长 7.组合数学 //Polya定理 8.模拟 9.数据结构 //并查集.堆 10.博弈论 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 2377, 2380, 1318, 1877, 1928, 1971,