转载请注明出处,http://blog.csdn.net/Bule_Zst/article/details/78054564
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
题意:给一个字符串(包含多个单词,用空格隔开),现在要将字符串分割成多行,规定,单词不能进行分割(即只能通过空格分割),问是否能分割
方法:枚举所有分割情况,一个一个判断,因为是顺序存储,所以对于每种情况检查起来特别简单
代码:
// @Team : nupt2017team12
// @Author : Zst
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
#define MOD 1000000007
#define CLR(a,x) memset(a,x,sizeof(a))
#define INF 0x3f3f3f3f
#define pb push_back
#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )
#define WHILE() int T;scanf( "%d", &T );while( T-- )
const int N = 1e5+7;
char str[N];
int len;
bool ans;
void solve() {
FOR( i, 2, len/2 ) {
int j = i;
bool flag = true;
while( j <= len ) {
if( str[j] != ' ' ) {
flag = false;
break;
}
j += i;
}
if( j-i != len ) {
flag = false;
}
if( flag ) {
ans = true;
return;
}
}
}
int main()
{
// freopen( "J.txt", "r", stdin );
int w;
scanf( "%d", &w );
getchar();
while( w-- ) {
ans = false;
str[0] = '0';
gets( str+1 );
len = strlen( str );
str[len] = ' ';
solve();
if( ans ) {
printf( "YES\n");
} else {
printf( "NO\n");
}
}
return 0;
}