java – timing out on one case, USACO 2018 US Open Contest, Silver Problem 3. Multiplayer Moo1

Referring to this question: http://usaco.org/index.php?page=viewproblem2&cpid=836

Hi! So I’m working on this problem, and my approach was to search ever possible ways, but it time out on one case:enter image description here

And here is my code:

import java.io.*;
import java.util.*;

public class main {
    public static int()() grid;
    public static boolean()() went, went2;
    public static int cnt;
    
    public static void dfs1(int x, int y, int wanted) {
        if(x < 0 || y < 0 || x >= grid.length ||  y >= grid.length) {
            return;
        }
        
        if(went(x)(y) || grid(x)(y) != wanted) {
            return;
        }
        went(x)(y) = true;
        cnt++;
        
        dfs1(x + 1, y, wanted);
        dfs1(x - 1, y, wanted);
        dfs1(x, y + 1, wanted);
        dfs1(x, y - 1, wanted);
    }
    
    public static void dfs2(int x, int y, LinkedList<Integer> wanted) {
        if(x < 0 || y < 0 || x >= grid.length || y >= grid.length) {
            return;
        }
        
        if(went2(x)(y)) {
            return;
        }else if(!wanted.contains(grid(x)(y)) && wanted.size() >= 2) {
            return;
        }else if(wanted.size() < 2 && !wanted.contains(grid(x)(y))) {
            wanted.add(grid(x)(y));
        }
        
        cnt++;
        if(wanted.size() < 2) {
            went(x)(y) = true;
        }
        went2(x)(y) = true;
        
        dfs2(x + 1, y, wanted);
        dfs2(x - 1, y, wanted);
        dfs2(x, y + 1, wanted);
        dfs2(x, y - 1, wanted);
    }
    
    
    public static void main(String() args) throws IOException{
        //BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader input = new BufferedReader(new FileReader("multimoo.in"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("multimoo.out")));
        
        StringTokenizer st = new StringTokenizer(input.readLine());
        int n = Integer.parseInt(st.nextToken());
        grid = new int(n)(n);
        went = new boolean(n)(n);
        went2 = new boolean(n)(n);
        
        for(int i = 0;i<n;i++) {
            st = new StringTokenizer(input.readLine());
            
            for(int j = 0;j<n;j++) {
                int num = Integer.parseInt(st.nextToken());
                grid(i)(j) = num;
            }
            
        }
        
        
        int best = 0;
        //int best2 = 0;
        
        for(int i = 0;i<n;i++) {
            for(int j = 0;j<n;j++) {
                if(!went(i)(j)) {
                    cnt = 0;
                    dfs1(i, j, grid(i)(j));
                    if(cnt > best) {
        //              best2 = best;
                        best = cnt;
                    }
                }
            }
        }
        
        //best2 += best;
        //System.out.println(best);
        out.println(best);
        best = 0;
        
        went = new boolean(n)(n);
        
        
        for(int i = 0;i<n;i++) {
            for(int j = 0;j<n;j++) {
                if(!went(i)(j)) {
                    cnt = 0;
                    LinkedList<Integer> wanted = new LinkedList<Integer>();
                    wanted.add(grid(i)(j));
                    went2 = new boolean(n)(n);
                    dfs2(i, j, wanted);
                    best = Math.max(best, cnt);
                }
            }
        }
        
        //System.out.println(best);
        out.println(best);
        out.close();
    }

}

So my question was, how can I pass the one case? I sure I just need a little change somewhere, since I’m only having a time limit exceeded on one case. Thanks!

security – For a very long lived PHP web application project, is custom (self made) framework better than (open source) popular frameworks?

There is a lot to unpack here. Let’s start with…

1. Security through obscurity

Obscurity in and of itself does not provide additional security, unless it is so utterly complete and total, that it barely balances being usable.

Are your company records secret?

Are you sure an attacker can’t find out who used to work for your devteam?

Are you sure none of ex- or current employees have any copies of the source code on their home machines?

Are you sure that they maintain secrecy and will not spill the beans willingly upon social engineering attempt?

Is your server running an OS that has no vulnerabilities?

Are you absolutely sure an attacker can’t get access directly to the server physically?

Is your team really THAT good and all the points of contact (which are exposed and therefore not obscured at all) in your framework are secure?

If you can’t answer a strong “YES” to every one of those questions (and many others), than obscurity is not going to help much.

Which brings us to…

2. More open means more secure

It is just a simple numbers game. There is a set percentage of security specialists amongst the population of developers in the world. If more people have access to your code—higher number of professionals will check it out and spot the problem.

This has been the working solution for pretty much every collaborative project in the past couple hundred years. That is how Wikipedia operates. And it is how most modern frameworks operate. They are open sourced in part due to security concerns. It allows them to prove that they are not hiding anything, and in turn, security professionals from all around the world have an opportunity to contribute their experience and skills to the creation of these frameworks.

The dedicated security solutions implemented in open source frameworks will beat you in terms of security. That is, unless you are absolutely sure that your team is able to achieve a world-class security in your custom-tailored closed-source project. Which brings me to…

3. Longevity of code

You have to rememember, that your framework is not floating through the void of space. Your code interacts with a lot of other systems.

You say that you use PHP5. It is now firmly in its “end of life” stage and will never be updated again. Are you comfortable using a system that has a bunch of well-known and widely publicized vulnerabilities? No matter how good your code is, and how strongly you obscure your system, those are not going anywhere.

When you say that PHP5 will still run on servers, do you imply that it will just work indefinitily? Because it most surely won’t. As the years go by, support for it will be dropped by major webservers, because there is no reason to drag all of this old, vulnerable, and barely used code along.

So unless you are going to keep your entire architecture the same for the next 50 years, your application is going to stop working as expected at some point. And if you do, then expect to have a lot more vulnerabilities to worry about.

4. Nothing lasts forever

Here is a list of things that did not exist 30 years ago: https, http cookies, JavaScript, PHP.

It is difficult to tell what the web will be like in 5–10, let alone 50 years. Requirements will change. Your application will have to adapt. If your team can pull off writing an web application that will live for so long, using a closed-source framework, based on technologies that are already dying—hats off to you.

productivity – Is there any way to show a timer that shows how long a chrome window has been open?

This would be useful for those wanting to track on long they’re spending on various tasks (assuming they start the task in a new chrome window). It’s simple and avoids the need to do any other manual logging.

A google search didn’t seem to provide useful information. An extension for this would be great, sadly searched chrome webstore but couldn’t find anything. Not sure if I missed something tho.

Thanks for any help

sharepoint online – unable to edit a power-point presentation. Error “We can not open this presentation because it is protected with IRM.”

We have a power-point presentation inside our online document library, but if the users try to edit the presentation using the browser they will get this error:-

enter image description here

and if i chose to open in the desktop app i will keep getting this message:-
enter image description here

Although when i access:- Admin centers >> SharePoint >> settings >> classic settings page >> In the Information Rights Management (IRM), i found that we are not using IRM, as follow:-

enter image description here

adb – Open port to all interfaces

I have an old machine with no built-in wifi adapter. I’m using a spare android device to connect this machine to my wifi home network via usb tethering. I’d like to ssh into this machine. The machine’s ip address is behind the usb-tethering subnet.

What I tried:

adb reverse tcp:2222 tcp:22

I’m able to ssh from an app running inside the android device (e.g. connectbot) into the machine. But when I try to connect from another device on the home network:

ssh -p2222 <android-device-ip-address>

I get connection refused. nmap reports that all ports in the android device are closed.

How can I open port 2222 to accept connections from outside the android device?

software installation – this message when open Android Studio

Internal error. Please refer to https://code.google.com/p/android/issues

java.lang.ExceptionInInitializerError
at com.a.a.b.a.p.a(p.java:182)
at com.a.a.b.b.ar.a(ar.java:348)
at com.a.a.b.b.ar.a(ar.java:172)
at com.a.a.b.b.ar.a(ar.java:71)
at com.intellij.idea.MainImpl.start(MainImpl.java:19)
at com.intellij.idea.StartupUtil.lambda$startApp$5(StartupUtil.java:248)
at com.intellij.util.ui.EdtInvocationManager.executeWithCustomManager(EdtInvocationManager.java:73)
at com.intellij.idea.StartupUtil.startApp(StartupUtil.java:243)
at com.intellij.idea.StartupUtil.prepareApp(StartupUtil.java:214)
at com.intellij.ide.plugins.MainRunner.lambda$start$0(MainRunner.java:45)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at com.intellij.ui.scale.JBUIScale.lambda$static$0(JBUIScale.java:72)
at com.intellij.openapi.util.AtomicNotNullLazyValue$1.compute(AtomicNotNullLazyValue.java:59)
at com.intellij.openapi.util.AtomicNotNullLazyValue.getValue(AtomicNotNullLazyValue.java:37)
at com.intellij.ui.scale.JBUIScale.lambda$static$1(JBUIScale.java:152)
at com.intellij.util.LazyInitializer$MutableNotNullValue.initialize(LazyInitializer.java:113)
at com.intellij.util.LazyInitializer$NullableValue.get(LazyInitializer.java:41)
at com.intellij.util.LazyInitializer$MutableNotNullValue.get(LazyInitializer.java:102)
at com.intellij.ui.scale.JBUIScale.lambda$static$2(JBUIScale.java:170)
at com.intellij.util.LazyInitializer$MutableNotNullValue.initialize(LazyInitializer.java:113)
at com.intellij.util.LazyInitializer$NullableValue.get(LazyInitializer.java:41)
at com.intellij.util.LazyInitializer$MutableNotNullValue.get(LazyInitializer.java:102)
at com.intellij.ui.scale.JBUIScale.scale(JBUIScale.java:294)
at com.intellij.util.ui.JBInsets.create(JBInsets.java:41)
at com.intellij.util.ui.UIUtil.(UIUtil.java:457)
… 11 more

design – how to architect a cli based program to: 1, grep through files, 2 show report of grep’d instance, 3 open file from that report in vim?

I’ve got a couple handy little scripts that I use with VimWiki (my digital diary/journal system is deeply envied by non-geek active journal’ers) …and I want to integrate these separate functions into a sleek workflow.

One of the features of the VimWiki is to be able to insert :tags: throughout the wiki pages (including diary pages).

The object here is to integrate a couple tag-related scripts:

  • Script 1 gives prints to the console (piped to less, actually) an alphabetized list of all the tags in my diary.
  • Script 2 gives me a report of one specific tag: each time the tag is mentioned in a diary page the report will include the date of that daily entry, the tag, and the notes/text that follow the tag.

The desired functionality is to be able to invoke script1 and scroll through the list, and then hit on a selected tag to be able to invoke script2 while providing the selected tag as an argument for script2.

Script1 is very simple, it generates a list of all the :tags: used throughout my daily diary entries:

grep -rh -e '^:S*:$' ~/Documents/wiki/diary/*.mkd  |
while read -r line
do
  for word in ${line//:/ }
  do
    printf "${word}n"
  done
done | sort -u | less

The grep produces content like:

:tech:
:therapy:babylon:
:trauma:
:triggered:
:truama:leadership:business:toxicity:
:unfurling:
:tagOne:tagTwo:etcTag:

and then in the while & for loops we render that as a simple list

babylon
business
etcTag
leadership
tagOne
tagTwo
tech
therapy
toxicity
trauma
triggered
truama
unfurling

But less doesn’t have the ability to “select” a line(the line contains the name of a tag), let alone the ability to magically supply the highlighted text as an argument to the next script.

The next script is still pretty simple, it shows me all the contents related to a given tag:

#!/usr/bin/python3
import things
tagname = ':' + sys.argv(1) + ':'
directory = '/home/alec/Documents/wiki/diary/'
for filename in sorted(os.listdir(directory)):
  if filename.startswith("20") and filename.endswith(".mkd"): 
    with open(directory + filename) as file:
      # find all the contents related to the "tagname" argument supplied to the script
      # this is truncated because that specific code isn't particularly relevant
      if len(tagcontent) != 0:
        print(date + ' ' + tagline.rstrip())
        print(tagcontent)

This script will show me content from my diary pages related to the tag, so diarytags.py culture will give some content like:

# Tuesday 03 November 2020 :culture:business:leadership:toxicity:
stuff that I wrote that day related to the toxic culture that sometimes exists among leadership of organizations and businesses
# Thursday 26 November 2020 :politics:culture:empathy:love:
something I wrote that day about the lack of or need for more empathy and love in the culture of our politics and governance

…and so forth, pretty slick as far as journals/diaries go 🙂

So how to use the first script sort of like a gui/browser (solely inside the terminal of course) for supplying an argument to the second script? And, optimally, which may(?) be significantly harder(?), to be able to navigate back to the results of the first script to select a different tag and view its report.

The final step would be to then open a journal entry from the report which, something like Tuesday 03 November 2020 would open the file in vim: vi ~/Documents/wiki/diary/2020-11-03.mkd.

How would you go about designing a program like this?


edit:

About the simplest architecture that could work in a hack-like fashion would be using vim as the pager (probably supplying a special vimrc file for it) and vimscript functions so that <Enter> keybinding could use the keyword as an argument for script2. <Backspace> could be used to delete the current buffer and thus arrive back at the list of tags (produced by script1). And from the report (script2) a another vimscript function could be used to load the diary page for the section beneath the mouse cursor.

That might be good enough for my purposes.

I’m interested in other simple solutions. I’m interested in learning what options exist for designing a program that moves beyond the simple scripts as I’ve described here.

rkhunter reports numerous warnings on haproxy open ports

Recently I have installed RKhunter (v1.4.2) on a couple of loadbalancers ( Haproxy 2.0.14 ) running on Debian 9. Stretch. While performing a full system check I’m getting a lot or warnings about tcp ports being used by Haproxy. They look like this:

         Use the 'lsof -i' or 'netstat -an' command to check this.
Warning: Network TCP port 13000 is being used by /usr/sbin/haproxy. Possible rootkit: Possible Universal Rootkit (URK) SSH server
         Use the 'lsof -i' or 'netstat -an' command to check this.
Warning: Network TCP port 47018 is being used by /usr/sbin/haproxy. Possible rootkit: Possible Universal Rootkit (URK) component
         Use the 'lsof -i' or 'netstat -an' command to check this.*

Also, it seems that I cannot simply whitelist those ports as they seem to keep changing.
What one would do in this case ?

intents – NFC: Spotify URIs open app, not specified resource

My goal is to program NFC tags in such a way, that Spotify starts playing a specific track/album/playlist.

But after writing a Spotify URI (e.g. spotify:track:6rqhFgbbKwnb9MLmUQDhG6) to my tag, it only opens the app and disregards the resource.

The problem seems to be the way Android handles the tag, because the URI works without any problems when I call it from another app.

A related problem is the way URLs are handled: When I open a Spotify URL like https://open.spotify.com/track/6rqhFgbbKwnb9MLmUQDhG6 from another app, it directly launches Spotify, whereas it would unsuccessfully try to open it with a browser after touching the tag.

It seems like Android only processes the scheme of the URI. Is this correct? If so, is there something that can be done to circumvent this? Why would that be the case?

Open ended questions or not for screener surveys?

I am preparing my first screener survey to recruit some participants for an interview.

I am getting mixed views on if these questions should be open ended or not.

For example, I have the following questions:

  • How often do you shop in Townsworth High Street? (Daily, Weekly, Monthly, Never)
  • How often do you attend events in Townsworth? (Daily, Weekly, Monthly, Never)
  • Do you prefer to “shop local” or support local businesses than a high street chain? (yes, no) (although I know I shouldn’t have yes/no questions, but not sure how I can reword this to get people who prefer to shop locally in the interview)
  • How often do you read reviews about a shop before visiting? (Always, Sometimes, Less Often, Never)
  • How often do you read reviews about a product before purchasing? (Always, Sometimes, Less Often, Never)

Should some of these be open ended questions such as

  • Tell me about the events you attend in Townsworth
  • Tell me why you prefer to shop local than high street chains

Should I be using open ended questions in the screener survey or leave these until the actual interview?

The NN Group mentions having open ended questions:
https://www.nngroup.com/articles/screening-questions-select-research-participants/

Any advice would be great, thanks!