首页 > ACM题库 > HDU-杭电 > HDU 2760-HOJ-Scrolling Sign[解题报告]C++
2014
02-14

HDU 2760-HOJ-Scrolling Sign[解题报告]C++

Scrolling Sign

问题描述 :

Electric scrolling signs are often used for advertising. A given sign displays exactly k characters. When the sign is switched on, all of the character positions are initially empty (showing spaces). In each time interval, all of the characters on the sign are shifted to the left by one position, and a new character is added at the right-most position. The character that was in the left-most position moves off the sign.

For certain sequences of words, it is possible to reuse characters from one word to form a subsequent word. For example, on a sign with three character positions, the sign can display the message CAT ATE TED by scrolling in the five characters CATED.

The advertiser has a specific message to show using the sign. The faster the message is displayed, the more people will be able to see the whole message. Therefore, your job is to find a way to display all the words of the message by scrolling in the smallest number of letters. In between showing the words of the message, the sign may display other words that are not considered part of the message. However, the words of the message must be shown in the order in which they are given.

输入:

The first line of test chunk contains a single integer n, the number of test cases in this chunk to follow. Each test case starts with a line containing a two integers, k, the number of character positions on the sign, and w, the number of words in the message. Each of the two integers is between 1 and 100, inclusive. The following w lines each contain a word of the message comprising exactly k uppercase letters.

Please process to the end of the data file.

输出:

The first line of test chunk contains a single integer n, the number of test cases in this chunk to follow. Each test case starts with a line containing a two integers, k, the number of character positions on the sign, and w, the number of words in the message. Each of the two integers is between 1 and 100, inclusive. The following w lines each contain a word of the message comprising exactly k uppercase letters.

Please process to the end of the data file.

样例输入:

2
3 2
CAT
TED
3 3
CAT
ATE
TEA
2
3 2
CAT
TED
3 3
CAT
ATE
TEA

样例输出:

5
5
5
5

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int T;
string str[105];
int l, n;

void work()
{
	scanf("%d%d\n", &l, &n);
	for (int i = 0; i < n; ++i) cin >> str[i];
	str[n] = str[0];
	int ans = n * l;
	string cur = str[0];
	for (int i = 1; i < n; ++i) {
		int common = 0;
		for (int cl = 1; cl <= l; ++cl) if (cur.substr(cur.size() - cl, cl) == str[i].substr(0, cl)) common = cl;
		cur += str[i].substr(common);
	}
	printf("%d\n", cur.size());
}
	
int main()
{
	while (scanf("%d", &T) != EOF)
		while (T--) work();
}

  1. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测

  2. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。