2020 Winter Holiday [gmoj1923] [scode password encoding] [DFS] [string]

Title description

Farmer John recently wanted to send some secret messages, but he did n’t want the cows to know. The information is composed of characters from 'A' to 'Z', and the length is at least 2.
In order to encrypt this information, Farmer John performed a series of operations on this information. In each operation, John removed the string S from the first number of consecutive characters or the last character from the last character ( Remove at least one character, but not all), and then add the remaining string to the left or right of the original S string. For example, for the string ABC, one operation can have 8 results:
AABC
ABABC
BCABC
CABC
ABCA
ABCAB
ABCBC
ABCC
now given the last encrypted string, John wants to know how many methods this string may be encrypted from, note AAA can be obtained by AA through four encryption operations. Although the generated AAA is the same, but the encryption process is different, we think it is a different method.

Input

A string.

Output

How many methods can be used to encrypt this output string. [Answer to mod 2014]

Sample input

ABABA

Sample output

8

Data range limitation

The length of the string does not exceed 100.

prompt

[Sample Description]
We have 8 ways to get ABABA:

  1. Start with ABA -> AB+ABA
  2. Start with ABA -> ABA+BA
  3. Start with AB -> AB+A -> AB+ABA
  4. Start with AB -> AB+A -> ABA+BA
  5. Start with BA -> A+BA -> AB+ABA
  6. Start with BA -> A + BA -> ABA + BA
  7. Start with ABAB -> ABAB+A
  8. Start with BABA -> A+BABA、

analysis

The main idea of ​​the question:
Find out how many changes a string can be obtained . The changes are: intercept several strings from the left (or from the right) (can delete one, not all), and place it on the left or right of the original string.

This question is a very strange DFS. .
Divide a string into a, b, ..., c, d
f = a + b + ...
e = ... + c + d
a for statement, we can know that the string we intercepted will be in the "original string" The corresponding string, if it exists, then it must be on the left or the right, it is impossible on the left, so we can continue to process e or f.

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
string str;
map<string,int>a;
int dfs(string s)
{
	if(a[s]) return a[s];//记忆化
	int ans=0,l;
	string ff,b,c,d,e,f;
	l=s.size();
	for(int i=0;(i+1)*2<l;i++)
	{
		ff=s.substr(0,i+1);
		b=s.substr(i+1,i+1);
		c=s.substr(l-i-1,i+1);
		d=s.substr(l-i-i-2,i+1);
		e=s.substr(i+1,l-i-1);
		f=s.substr(0,l-i-1);
		if(ff==b) ans+=dfs(e);
		if(ff==c) ans+=dfs(e);
		if(c==ff) ans+=dfs(f);
		if(c==d) ans+=dfs(f);
	}
	if(l>=2) ans++;
	ans%=2014;
	a[s]=ans;
	return ans;
}
int main()
{
    freopen("scode.in","r",stdin);
    freopen("scode.out","w",stdout);
    cin>>str;
    cout<<dfs(str)-1;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Published 110 original articles · 100 praises · 8020 visits

Guess you like

Origin blog.csdn.net/dglyr/article/details/104931572