import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;
class Ideone
{
{
String s
= "This is a test\nAnd this is also a test\nAnd these are also tests\ntest\nЭто тест\nЭто также тест\nИ это также тесты"; String rx
= "(?sU)\\b(\\w+)\\b(?=.*\\b\\1\\b)"; List<String> matches = new ArrayList<>();
Matcher m = Pattern.compile(rx).matcher(s);
while (m.find()) {
matches.add(m.group());
}
System.
out.
println(s.
replaceAll("(?U)\\b(?:" + String.
join("|", matches
) + ")\\b",
"_$0")); }
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS51dGlsLnJlZ2V4Lio7CmltcG9ydCBqYXZhLmxhbmcuKjsKaW1wb3J0IGphdmEuaW8uKjsKCmNsYXNzIElkZW9uZQp7CglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KCXsKCQlTdHJpbmcgcyA9ICJUaGlzIGlzIGEgdGVzdFxuQW5kIHRoaXMgaXMgYWxzbyBhIHRlc3RcbkFuZCB0aGVzZSBhcmUgYWxzbyB0ZXN0c1xudGVzdFxu0K3RgtC+INGC0LXRgdGCXG7QrdGC0L4g0YLQsNC60LbQtSDRgtC10YHRglxu0Jgg0Y3RgtC+INGC0LDQutC20LUg0YLQtdGB0YLRiyI7CgkJU3RyaW5nIHJ4ID0gIig/c1UpXFxiKFxcdyspXFxiKD89LipcXGJcXDFcXGIpIjsKCQlMaXN0PFN0cmluZz4gbWF0Y2hlcyA9IG5ldyBBcnJheUxpc3Q8PigpOwoJCU1hdGNoZXIgbSA9IFBhdHRlcm4uY29tcGlsZShyeCkubWF0Y2hlcihzKTsKCQl3aGlsZSAobS5maW5kKCkpIHsKCQkJbWF0Y2hlcy5hZGQobS5ncm91cCgpKTsKCQl9CgkJU3lzdGVtLm91dC5wcmludGxuKHMucmVwbGFjZUFsbCgiKD9VKVxcYig/OiIgKyBTdHJpbmcuam9pbigifCIsIG1hdGNoZXMpICsgIilcXGIiLCAiXyQwIikpOwoJfQp9