UVA 455 Periodic Strings string的一些方法的用法

题目描述:

A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one repetition of ”abcabcabcabc”). Write a program to read a character string and determine its smallest period.
Input
The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.
Output
An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.
Sample Input
1
HoHoHo
Sample Output
2

PDF文件复制粘贴会把一段分成多个段落,所以还是看原题比较好

原题戳我

今天随机出来L1-001 不知道怎么回事,总是过不了,绝望,没时间了,明天也是四节大课,懵逼,写个水题换换心情

这个题其实也不是脸滚键盘找的,是算法竞赛入门经典的绿皮书..上面讲解都挺好的,感觉智商不够用

中文意思是:如果一个字符串可以由某个长度为k的字符串重复多次得到,就可以说该串以k为周期.
例如:abcabcabcabc以3为周期

输入一个长度不超过80的字符串,输出他的最小周期

这个题书中倒是已经给了解释,不过用string的一些方法会更方便一些.

首先就是周期肯定是一个能被整除的数,也就是说肯定是有限的,那我们就从1开始 不断往上走,直到为字符串长度len

int ans=1;
int len=s.length();
for(;ans<=len;ans++){

由于ans在下面还要用到 所以就不在for循环里定义

了解一下部分string的用法
1.erase方法

  • 如果为空则清空整个字符串s.erase()
s.erase();

2.data方法

  • 返回字符串的字符数组形式
s.data();

3.assign方法

string s1,s2;
s1.assign(s2);
s1.assign(s2.data(),num);
  • 为字符串赋值
  • 用另一字符串中前num个字符串赋值

4.append方法

  • 在字符串的末尾添加东西
string s1,s2;
s2.append(s1);

首先erase方法肯定是用来清零的
data方法主要是用在assign方法中,因为他变量中是字符数组形式而不是string类型
append是用来模拟多个字符节拼接在一起,判断是否与原字符串相等,如果相等则退出循环 输出结果,否则则循环节长度+1,重新计算

至于时间复杂度方面,由于这个题样例似乎有点水,都是0ms,没能看出来结果
代码如下:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
// #define Kongxiangzhouye
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define _rep(i,a,b) for(int i=(a),i<=(b);++i)
#define _for(i,a,b) for(int i=(a),i<(b);++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;


int main()
{
    #ifdef Kongxiangzhouye
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    string s;
    string test1;
    string test2;
    while(n--){
        cin>>s;
        int ans=1;
        int len=s.length();
        for(;ans<=len;ans++){
            test1.erase();
            test2.erase();
            if(len%ans)
                continue;
            int temp=len/ans;
            test1.assign(s.data(),ans);
            for(int i=0;i<temp;i++){
                test2.append(test1);
            }
            if(test2==s)
                break;
        }
        cout<<ans<<endl;
        if(n)
            cout<<endl;
    }
    return 0;
}






猜你喜欢

转载自blog.csdn.net/qq_38842456/article/details/79547097