NC15833. Red Rover
描述
输入描述
Input consists of a single line containing a string made up of the letters N, S, E, and W representing the route to transmit to the rover. The maximum length of the string is 100.
输出描述
Display the minimum number of characters needed to encode the route.
示例1
输入:
WNEENWEENEENE
输出:
10
C++11(clang++ 3.9) 解法, 执行用时: 13ms, 内存消耗: 1528K, 提交时间: 2020-02-28 23:03:47
#include<bits/stdc++.h> using namespace std; int n; int res; string s; string a; map<string,int>S; map<string,int>jsq; int main() { cin>>s; res=n=s.size(); for(int i=0;i<n;i++) { a=s[i]; for(int j=i+1;j<n;j++) { a+=s[j]; if(S[a]<=i) jsq[a]++,S[a]=j+1; res=min(res,n-(j-i)*(jsq[a]-1)+1); } } cout<<res<<endl; return 0; }
Python3(3.5.2) 解法, 执行用时: 34ms, 内存消耗: 3456K, 提交时间: 2018-05-03 20:39:07
s = input() n = len(s) ans = n for i in range(2, len(s) + 1): for j in range(n - i + 1): sp = s[j:j+i] ans = min(ans, n - s.count(sp) * (i - 1) + i) print(ans)