Created
March 27, 2016 20:17
-
-
Save jianminchen/59e326cbd1d8910c01c7 to your computer and use it in GitHub Desktop.
Sherlocks and Anagram - C# use Dictionary class, sort the string, anagram string should be the same after sorting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
class TEST{ | |
static void Main(){ | |
Sol mySol =new Sol(); | |
mySol.Solve(); | |
} | |
} | |
class Sol{ | |
public void Solve(){ | |
for(int t=0;t<T;t++){ | |
Dictionary<String,int> D=new Dictionary<String,int>(); | |
for(int l=1;l<=S[t].Length;l++){ | |
for(int i=0;i+l-1<S[t].Length;i++){ | |
String ss=S[t].Substring(i,l); | |
var ca=ss.ToCharArray(); | |
Array.Sort(ca); | |
var sss=new String(ca); | |
if(!D.ContainsKey(sss))D.Add(sss,0); | |
D[sss]++; | |
} | |
} | |
long ans=0; | |
foreach(var s in D.Keys){ | |
ans+=D[s]*(D[s]-1)/2; | |
} | |
Console.WriteLine(ans); | |
} | |
} | |
int T; | |
String[] S; | |
public Sol(){ | |
T=ri(); | |
S=new String[T]; | |
for(int i=0;i<T;i++)S[i]=rs(); | |
} | |
static String rs(){return Console.ReadLine();} | |
static int ri(){return int.Parse(Console.ReadLine());} | |
static long rl(){return long.Parse(Console.ReadLine());} | |
static double rd(){return double.Parse(Console.ReadLine());} | |
static String[] rsa(){return Console.ReadLine().Split(' ');} | |
static int[] ria(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>int.Parse(e));} | |
static long[] rla(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>long.Parse(e));} | |
static double[] rda(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>double.Parse(e));} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment