zoukankan      html  css  js  c++  java
  • 矿大OJ 1768.Power Strings.

    题目描述

    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 = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

    输入

    Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

    输出

    For each s you should print the largest n such that s = a^n for some string a.

    样例输入

    abcd
    aaaa
    ababab
    .

    样例输出

    1
    4
    3


    好像弄成英文看起来就变得高级了 其实就是求emmm.一时间我也不好描述..代码如下
     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int test(string s)
     5 {
     6     char a = s[0];
     7     int locate = s.find(a, 1);
     8     while(locate > 0)
     9     {
    10         bool flag = false;
    11         int len = s.length();
    12         int times =  len/ locate;
    13         if (len % locate == 0)
    14         {
    15             flag = true;
    16             for (int i = locate;i < len&&flag==true;i += locate)
    17             {
    18                 for (int j = 0;j <= locate - 1;j++)
    19                 {
    20                     if (s[j] != s[i + j])
    21                     {
    22                         flag = false;
    23                         break;
    24                     }
    25                 }
    26 
    27             }
    28             
    29         }    
    30         if(flag)
    31             return times;    
    32         else
    33             locate = s.find(a, locate + 1);
    34     }
    35     return 1;
    36 }
    37 
    38 int main()
    39 {
    40     string s;
    41     while (cin >> s && s != ".")
    42     {
    43         cout << test(s) << endl;
    44     }
    45     return 0;
    46 }

    这个做了我差不多5个小时..据说好像用什么KMP一些就做出来,现在还没学也看不懂,等到时候接触了再说吧




  • 相关阅读:
    [杂说]网络是基础生产工具
    这几天的工作
    [代码]大家来动动脑筋吧
    测试
    [基础] 如何使用extern和static限定符
    元宵节快乐
    复杂的“人"
    C# SMTP发邮件不支持465端口的解决方案,网易企业邮箱
    软件三层架构模型
    ASP.NET MVC 使用二级域名来注册Area区域
  • 原文地址:https://www.cnblogs.com/yuuuuu422/p/12822671.html
Copyright © 2011-2022 走看看