Spilt the String Gym - 101498J (枚举)

题目链接:https://vjudge.net/problem/Gym-101498J

题目:
Given a string s consisting of lowercase English letters and spaces, find a way to split the string s into multiple lines of the same length. Leading and trailing spaces are ignored while computing the length of the new lines.

Note that you cannot split words, for example if s contains the word “amman”, the whole word must be on the same line.

If there is a way to split s into multiple lines print “YES” (without the quotes), otherwise print “NO” (without the quotes).

Input
The first line of the input contains an integer T (1  ≤  T  ≤  250), where T is the number of the test cases.

Each case has one line that contains a string s. All strings are non-empty and the length of each of these strings does not exceed 105 characters.

It is guaranteed that there is only one space between any two words, and there are no leading or trailing spaces in s.

Output
Print T lines, on each line print “YES” (without the quotes) if you can split the given string s into multiple lines of the same length. Otherwise, print “NO” (without the quotes).

Example
Input
2
acm arab collegiate programming contest
acm amman collegiate programming contest
Output
YES
NO

题意: 输入一行字符串,每一行包括一些单词和空格,问你能不能把这个字符串分割为长度相等的几部分,不能从单词中间分割,只能从空格处分割,分割次数不限。

思路: 直接枚举分割的位置,看是否都是空格。

详情见代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <map>
using namespace std;
const int maxn =100000+5;
char s[maxn];
bool ans;
int len;

void solve()
{
    for(int i=2; i<=len/2; i++)
    {
        int j;
        for(j=i; j<=len; j+=i)
        {
            if(s[j]==' ')
            {
                continue;
            }
            else
                break;
        }
        if(j-i==len)
        {
            ans=1;
            return;
        }
    }
    return;
}


int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        ans=0;
        s[0]=' ';
        gets(s+1);
        len=strlen(s);
        s[len]=' ';
         solve();
         if(ans)
            printf("YES\n");
         else
            printf("NO\n");
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值